-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
124 lines (100 loc) · 2.75 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
122
123
124
package server
import (
"context"
"fmt"
"time"
"github.com/dgrijalva/jwt-go"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"github.com/go-rfe/gpwd/internal/logging/log"
pb "github.com/go-rfe/gpwd/internal/proto"
)
func (s *server) GenerateJWT(auth *pb.Auth) (string, error) {
claims := jwt.StandardClaims{
Subject: auth.GetUsername(),
ExpiresAt: time.Now().Add(s.cfg.TokenLifespan).Unix(),
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(s.secretKey)
}
func (s *server) VerifyJWT(accessToken string) (string, error) {
token, err := jwt.ParseWithClaims(
accessToken,
&jwt.StandardClaims{},
func(token *jwt.Token) (interface{}, error) {
_, ok := token.Method.(*jwt.SigningMethodHMAC)
if !ok {
return nil, fmt.Errorf("unexpected token signing method")
}
return s.secretKey, nil
},
)
if err != nil {
return "", fmt.Errorf("invalid token: %w", err)
}
claims, ok := token.Claims.(*jwt.StandardClaims)
if !ok {
return "", fmt.Errorf("invalid token claims")
}
return claims.Subject, nil
}
func (s *server) authUnaryInterceptor(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
if err := s.authorize(ctx, info.FullMethod); err != nil {
return nil, err
}
return handler(ctx, req)
}
func (s *server) authStreamInterceptor(
srv interface{},
stream grpc.ServerStream,
info *grpc.StreamServerInfo,
handler grpc.StreamHandler,
) error {
if err := s.authorize(stream.Context(), info.FullMethod); err != nil {
return err
}
return handler(srv, stream)
}
func (s *server) authorize(ctx context.Context, method string) error {
log.Debug().Msg(method)
if method == "/proto.Login/RegisterAccount" || method == "/proto.Login/Login" {
return nil
}
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return status.Errorf(codes.Unauthenticated, "metadata is not provided")
}
values := md["jwt"]
if len(values) == 0 {
return status.Errorf(codes.Unauthenticated, "authorization token is not provided")
}
accessToken := values[0]
_, err := s.VerifyJWT(accessToken)
if err != nil {
return status.Errorf(codes.Unauthenticated, "access token is invalid: %v", err)
}
return nil
}
func (s *server) mustReturnUsernameFromContext(ctx context.Context) string {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
log.Fatal().Msg("BUG: cannot get username from context")
}
values := md["jwt"]
if len(values) == 0 {
log.Fatal().Msg("BUG: cannot get username from context")
}
accessToken := values[0]
username, err := s.VerifyJWT(accessToken)
if err != nil {
log.Fatal().Msg("BUG: cannot get username from context")
}
return username
}