forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_session.go
133 lines (114 loc) · 4.85 KB
/
api_session.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
125
126
127
128
129
130
131
132
133
// Copyright 2021 The Nakama 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 server
import (
"context"
"github.com/gofrs/uuid"
"github.com/heroiclabs/nakama-common/api"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)
func (s *ApiServer) SessionRefresh(ctx context.Context, in *api.SessionRefreshRequest) (*api.Session, error) {
// Before hook.
if fn := s.runtime.BeforeSessionRefresh(); fn != nil {
beforeFn := func(clientIP, clientPort string) error {
result, err, code := fn(ctx, s.logger, "", "", nil, 0, clientIP, clientPort, in)
if err != nil {
return status.Error(code, err.Error())
}
if result == nil {
// If result is nil, requested resource is disabled.
s.logger.Warn("Intercepted a disabled resource.", zap.Any("resource", ctx.Value(ctxFullMethodKey{}).(string)))
return status.Error(codes.NotFound, "Requested resource was not found.")
}
in = result
return nil
}
// Execute the before function lambda wrapped in a trace for stats measurement.
err := traceApiBefore(ctx, s.logger, s.metrics, ctx.Value(ctxFullMethodKey{}).(string), beforeFn)
if err != nil {
return nil, err
}
}
if in.Token == "" {
return nil, status.Error(codes.InvalidArgument, "Refresh token is required.")
}
userID, username, vars, err := SessionRefresh(ctx, s.logger, s.db, s.config, s.sessionCache, in.Token)
if err != nil {
return nil, err
}
// Use updated vars if they are provided, otherwise use existing ones from refresh token.
useVars := in.Vars
if useVars == nil {
useVars = vars
}
userIDStr := userID.String()
token, exp := generateToken(s.config, userIDStr, username, useVars)
s.sessionCache.Add(userID, exp, token, 0, "")
session := &api.Session{Created: false, Token: token, RefreshToken: in.Token}
// After hook.
if fn := s.runtime.AfterSessionRefresh(); fn != nil {
afterFn := func(clientIP, clientPort string) error {
return fn(ctx, s.logger, userIDStr, username, useVars, exp, clientIP, clientPort, session, in)
}
// Execute the after function lambda wrapped in a trace for stats measurement.
traceApiAfter(ctx, s.logger, s.metrics, ctx.Value(ctxFullMethodKey{}).(string), afterFn)
}
return session, nil
}
func (s *ApiServer) SessionLogout(ctx context.Context, in *api.SessionLogoutRequest) (*emptypb.Empty, error) {
userID := ctx.Value(ctxUserIDKey{}).(uuid.UUID)
// Before hook.
if fn := s.runtime.BeforeSessionLogout(); fn != nil {
beforeFn := func(clientIP, clientPort string) error {
result, err, code := fn(ctx, s.logger, userID.String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxVarsKey{}).(map[string]string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, in)
if err != nil {
return status.Error(code, err.Error())
}
if result == nil {
// If result is nil, requested resource is disabled.
s.logger.Warn("Intercepted a disabled resource.", zap.Any("resource", ctx.Value(ctxFullMethodKey{}).(string)), zap.String("uid", userID.String()))
return status.Error(codes.NotFound, "Requested resource was not found.")
}
in = result
return nil
}
// Execute the before function lambda wrapped in a trace for stats measurement.
err := traceApiBefore(ctx, s.logger, s.metrics, ctx.Value(ctxFullMethodKey{}).(string), beforeFn)
if err != nil {
return nil, err
}
}
if err := SessionLogout(s.config, s.sessionCache, userID, in.Token, in.RefreshToken); err != nil {
if err == ErrSessionTokenInvalid {
return nil, status.Error(codes.InvalidArgument, "Session token invalid.")
}
if err == ErrRefreshTokenInvalid {
return nil, status.Error(codes.InvalidArgument, "Refresh token invalid.")
}
s.logger.Error("Error processing session logout.", zap.Error(err))
return nil, status.Error(codes.Internal, "Error processing session logout.")
}
// After hook.
if fn := s.runtime.AfterSessionLogout(); fn != nil {
afterFn := func(clientIP, clientPort string) error {
return fn(ctx, s.logger, userID.String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxVarsKey{}).(map[string]string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, in)
}
// Execute the after function lambda wrapped in a trace for stats measurement.
traceApiAfter(ctx, s.logger, s.metrics, ctx.Value(ctxFullMethodKey{}).(string), afterFn)
}
return &emptypb.Empty{}, nil
}