/
handler.go
112 lines (97 loc) · 3.91 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2017 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package auth
import (
"net/http"
restful "github.com/emicklei/go-restful"
authApi "github.com/kubernetes/dashboard/src/app/backend/auth/api"
kdErrors "github.com/kubernetes/dashboard/src/app/backend/errors"
"github.com/kubernetes/dashboard/src/app/backend/validation"
)
// AuthHandler manages all endpoints related to dashboard auth, such as login.
type AuthHandler struct {
manager authApi.AuthManager
}
// Install creates new endpoints for dashboard auth, such as login. It allows user to log in to dashboard using
// one of the supported methods. See AuthManager and Authenticator for more information.
func (self AuthHandler) Install(ws *restful.WebService) {
ws.Route(
ws.POST("/login").
To(self.handleLogin).
Reads(authApi.LoginSpec{}).
Writes(authApi.AuthResponse{}))
ws.Route(
ws.GET("/login/status").
To(self.handleLoginStatus).
Writes(validation.LoginStatus{}))
ws.Route(
ws.POST("/token/refresh").
Reads(authApi.TokenRefreshSpec{}).
To(self.handleJWETokenRefresh).
Writes(authApi.AuthResponse{}))
ws.Route(
ws.GET("/login/modes").
To(self.handleLoginModes).
Writes(authApi.LoginModesResponse{}))
ws.Route(
ws.GET("/login/skippable").
To(self.handleLoginSkippable).
Writes(authApi.LoginSkippableResponse{}))
}
func (self AuthHandler) handleLogin(request *restful.Request, response *restful.Response) {
loginSpec := new(authApi.LoginSpec)
if err := request.ReadEntity(loginSpec); err != nil {
response.AddHeader("Content-Type", "text/plain")
response.WriteErrorString(kdErrors.HandleHTTPError(err), err.Error()+"\n")
return
}
loginResponse, err := self.manager.Login(loginSpec)
if err != nil {
response.AddHeader("Content-Type", "text/plain")
response.WriteErrorString(kdErrors.HandleHTTPError(err), err.Error()+"\n")
return
}
response.WriteHeaderAndEntity(http.StatusOK, loginResponse)
}
func (self *AuthHandler) handleLoginStatus(request *restful.Request, response *restful.Response) {
response.WriteHeaderAndEntity(http.StatusOK, validation.ValidateLoginStatus(request))
}
func (self *AuthHandler) handleJWETokenRefresh(request *restful.Request, response *restful.Response) {
tokenRefreshSpec := new(authApi.TokenRefreshSpec)
if err := request.ReadEntity(tokenRefreshSpec); err != nil {
response.AddHeader("Content-Type", "text/plain")
response.WriteErrorString(kdErrors.HandleHTTPError(err), err.Error()+"\n")
return
}
refreshedJWEToken, err := self.manager.Refresh(tokenRefreshSpec.JWEToken)
if err != nil {
response.AddHeader("Content-Type", "text/plain")
response.WriteErrorString(kdErrors.HandleHTTPError(err), err.Error()+"\n")
return
}
response.WriteHeaderAndEntity(http.StatusOK, &authApi.AuthResponse{
JWEToken: refreshedJWEToken,
Errors: make([]error, 0),
})
}
func (self *AuthHandler) handleLoginModes(request *restful.Request, response *restful.Response) {
response.WriteHeaderAndEntity(http.StatusOK, authApi.LoginModesResponse{Modes: self.manager.AuthenticationModes()})
}
func (self *AuthHandler) handleLoginSkippable(request *restful.Request, response *restful.Response) {
response.WriteHeaderAndEntity(http.StatusOK, authApi.LoginSkippableResponse{Skippable: self.manager.AuthenticationSkippable()})
}
// NewAuthHandler created AuthHandler instance.
func NewAuthHandler(manager authApi.AuthManager) AuthHandler {
return AuthHandler{manager: manager}
}