/
handler.go
90 lines (76 loc) · 3.17 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
// 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 (
"github.com/gin-gonic/gin"
authApi "github.com/ycyxuehan/dashboard-gin/backend/auth/api"
"github.com/ycyxuehan/dashboard-gin/backend/errors"
"github.com/ycyxuehan/dashboard-gin/backend/validation"
"github.com/ycyxuehan/dashboard-gin/backend/utils/httphelper"
)
// 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 (a AuthHandler) Install(router *gin.RouterGroup) {
router.POST("/login", a.handleLogin)
router.GET("/login/status", a.handleLoginStatus)
router.POST("/token/refresh", a.handleJWETokenRefresh)
router.GET("/login/modes", a.handleLoginModes)
router.GET("/login/skippable", a.handleLoginSkippable)
}
func (a AuthHandler) handleLogin(c *gin.Context) {
loginSpec := new(authApi.LoginSpec)
if err := httphelper.ReadRequestBody(c, &loginSpec); err != nil {
httphelper.RestfullResponse(c, errors.HandleHTTPError(err), err)
return
}
loginResponse, err := a.manager.Login(loginSpec)
if err != nil {
httphelper.RestfullResponse(c, errors.HandleHTTPError(err), err)
return
}
httphelper.RestfullResponse(c, httphelper.SUCCESS, loginResponse)
}
func (a *AuthHandler) handleLoginStatus(c *gin.Context) {
httphelper.RestfullResponse(c, httphelper.SUCCESS, validation.ValidateLoginStatus(c))
}
func (a *AuthHandler) handleJWETokenRefresh(c *gin.Context) {
tokenRefreshSpec := new(authApi.TokenRefreshSpec)
if err := httphelper.ReadRequestBody(c, &tokenRefreshSpec); err != nil {
httphelper.RestfullResponse(c, errors.HandleHTTPError(err), err)
return
}
refreshedJWEToken, err := a.manager.Refresh(tokenRefreshSpec.JWEToken)
if err != nil {
httphelper.RestfullResponse(c, errors.HandleHTTPError(err), err)
return
}
httphelper.RestfullResponse(c, httphelper.SUCCESS, &authApi.AuthResponse{
JWEToken: refreshedJWEToken,
Errors: make([]error, 0),
})
}
func (a *AuthHandler) handleLoginModes(c *gin.Context) {
httphelper.RestfullResponse(c, httphelper.SUCCESS, authApi.LoginModesResponse{Modes: a.manager.AuthenticationModes()})
}
func (a *AuthHandler) handleLoginSkippable(c *gin.Context) {
httphelper.RestfullResponse(c, httphelper.SUCCESS, authApi.LoginSkippableResponse{Skippable: a.manager.AuthenticationSkippable()})
}
// NewAuthHandler created AuthHandler instance.
func NewAuthHandler(manager authApi.AuthManager) AuthHandler {
return AuthHandler{manager: manager}
}