generated from calmato/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
121 lines (97 loc) · 2.55 KB
/
auth.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
113
114
115
116
117
118
119
120
121
package util
import (
"fmt"
"strings"
"github.com/calmato/gran-book/api/internal/gateway/entity"
"github.com/calmato/gran-book/api/pkg/exception"
"github.com/calmato/gran-book/api/pkg/firebase/authentication"
"github.com/calmato/gran-book/api/proto/user"
"github.com/gin-gonic/gin"
)
type Authenticator interface {
Authentication() gin.HandlerFunc
Authorization() gin.HandlerFunc
HasAdminRole() gin.HandlerFunc
}
type authenticator struct {
auth *authentication.Auth
api user.AuthServiceClient
}
func NewAuthenticator(fa *authentication.Auth) Authenticator {
return &authenticator{
auth: fa,
}
}
func (a *authenticator) Authentication() gin.HandlerFunc {
return func(ctx *gin.Context) {
token, err := a.getToken(ctx)
if err != nil {
ErrorHandling(ctx, exception.ErrUnauthorized.New(err))
return
}
userID, err := a.auth.VerifyIDToken(ctx, token)
if err != nil || userID == "" {
ErrorHandling(ctx, exception.ErrUnauthorized.New(err))
return
}
a.setToken(ctx, token)
a.setAuth(ctx, userID, entity.RoleUser)
ctx.Next()
}
}
func (a *authenticator) Authorization() gin.HandlerFunc {
return func(ctx *gin.Context) {
auth, err := a.getAuth(ctx)
if err != nil {
ErrorHandling(ctx, exception.ErrForbidden.New(err))
return
}
if auth.Role() == entity.RoleUser {
ErrorHandling(ctx, exception.ErrForbidden.New(err))
return
}
a.setAuth(ctx, auth.Id, auth.Role())
ctx.Next()
}
}
func (a *authenticator) HasAdminRole() gin.HandlerFunc {
return func(ctx *gin.Context) {
auth, err := a.getAuth(ctx)
if err != nil {
ErrorHandling(ctx, exception.ErrForbidden.New(err))
return
}
if auth.Role() != entity.RoleAdmin {
ErrorHandling(ctx, exception.ErrForbidden.New(err))
return
}
a.setAuth(ctx, auth.Id, auth.Role())
ctx.Next()
}
}
func (a *authenticator) getAuth(ctx *gin.Context) (*entity.Auth, error) {
out, err := a.api.GetAuth(ctx, &user.Empty{})
if err != nil {
return nil, err
}
return entity.NewAuth(out.Auth), nil
}
func (a *authenticator) setAuth(ctx *gin.Context, userID string, role entity.Role) {
if userID != "" {
ctx.Set("userId", userID)
}
if role != entity.RoleUser {
ctx.Set("role", role)
}
}
func (a *authenticator) setToken(ctx *gin.Context, token string) {
ctx.Set("Authorization", fmt.Sprintf("Bearer %s", token))
}
func (a *authenticator) getToken(ctx *gin.Context) (string, error) {
token := ctx.GetHeader("Authorization")
if token == "" {
return "", fmt.Errorf("authorization header is not contain")
}
t := strings.Replace(token, "Bearer ", "", 1)
return t, nil
}