@@ -64,24 +64,21 @@ func (a *Authenticator) GetOrCreateUser(ctx context.Context) (*model.User, error
64
64
65
65
session := sessions .Get (ctx , a .sessionName )
66
66
67
- if session .Values [userIDKey ] == nil {
67
+ user , err := a .getCurrentUser (ctx )
68
+ if errors .Is (err , errNotFound ) {
68
69
// Create new user since UserID for cookie has not been created yet
69
70
user , err := a .createNewUser ()
70
71
if err != nil {
71
72
return nil , errors .Wrap (err , "failed to create new user" )
72
73
}
73
74
74
75
session .Values [userIDKey ] = user .ID .String ()
75
-
76
76
err = sessions .Save (ctx , session )
77
77
if err != nil {
78
78
fmt .Println ("Failed to save session!" )
79
79
return nil , errors .Wrap (err , "failed to save userID to session" )
80
80
}
81
- }
82
-
83
- user , err := a .getCurrentUser (session .Values [userIDKey ].(string ))
84
- if err != nil {
81
+ } else if err != nil {
85
82
sentry .CaptureException (errors .New (fmt .Sprintf (
86
83
"Failed to load user id %s from session\n " , session .Values [userIDKey ].(string ))))
87
84
return nil , errors .New ("failed to load user id from session" )
@@ -90,6 +87,23 @@ func (a *Authenticator) GetOrCreateUser(ctx context.Context) (*model.User, error
90
87
return user , nil
91
88
}
92
89
90
+ func (a * Authenticator ) GetUser (ctx context.Context ) (* model.User , error ) {
91
+ a .lock .Lock ()
92
+ defer a .lock .Unlock ()
93
+
94
+ session := sessions .Get (ctx , a .sessionName )
95
+ user , err := a .getCurrentUser (ctx )
96
+ if err != nil {
97
+ if ! errors .Is (err , errNotFound ) {
98
+ sentry .CaptureException (errors .New (fmt .Sprintf (
99
+ "Failed to load user id %s from session\n " , session .Values [userIDKey ].(string ))))
100
+ }
101
+ return nil , fmt .Errorf ("failed to load user id from session: %w" , err )
102
+ }
103
+
104
+ return user , nil
105
+ }
106
+
93
107
// CheckProjectAccess returns an error if the current user is not authorized to mutate
94
108
// the provided project.
95
109
//
@@ -105,7 +119,7 @@ func (a *Authenticator) CheckProjectAccess(ctx context.Context, proj *model.Proj
105
119
return errors .New ("no userIdKey found in session" )
106
120
}
107
121
108
- user , err := a .getCurrentUser (session . Values [ userIDKey ].( string ) )
122
+ user , err := a .getCurrentUser (ctx )
109
123
if err != nil {
110
124
return errors .New ("access denied" )
111
125
}
@@ -138,11 +152,23 @@ func (a *Authenticator) CheckProjectAccess(ctx context.Context, proj *model.Proj
138
152
return errors .New ("access denied" )
139
153
}
140
154
141
- func (a * Authenticator ) getCurrentUser (userIDStr string ) (* model.User , error ) {
155
+ var errNotFound = errors .New ("user not found" )
156
+
157
+ // getCurrentUser from the request context using the session to get the user ID
158
+ // and retrieving that user from the storage.
159
+ // expected errors:
160
+ // - errNotFound user was not created
161
+ func (a * Authenticator ) getCurrentUser (ctx context.Context ) (* model.User , error ) {
162
+ session := sessions .Get (ctx , a .sessionName )
163
+ if session .Values [userIDKey ] == nil {
164
+ return nil , errNotFound
165
+ }
166
+ rawUserID := session .Values [userIDKey ].(string )
167
+
142
168
var user model.User
143
169
var userID uuid.UUID
144
170
145
- err := userID .UnmarshalText ([]byte (userIDStr ))
171
+ err := userID .UnmarshalText ([]byte (rawUserID ))
146
172
if err != nil {
147
173
return nil , errors .Wrap (err , "failed to unmarshal userIDStr" )
148
174
}
0 commit comments