@@ -54,7 +54,7 @@ impl AuthCheck {
54
54
}
55
55
}
56
56
57
- pub fn check < B > ( & self , request : & Request < B > ) -> AppResult < AuthenticatedUser > {
57
+ pub fn check < B > ( & self , request : & Request < B > ) -> AppResult < Authentication > {
58
58
let auth = authenticate_user ( request) ?;
59
59
60
60
if let Some ( token) = auth. api_token ( ) {
@@ -111,30 +111,47 @@ impl AuthCheck {
111
111
}
112
112
113
113
#[ derive( Debug ) ]
114
- pub struct AuthenticatedUser {
114
+ pub enum Authentication {
115
+ Cookie ( CookieAuthentication ) ,
116
+ Token ( TokenAuthentication ) ,
117
+ }
118
+
119
+ #[ derive( Debug ) ]
120
+ pub struct CookieAuthentication {
115
121
user : User ,
116
- token : Option < ApiToken > ,
117
122
}
118
123
119
- impl AuthenticatedUser {
124
+ #[ derive( Debug ) ]
125
+ pub struct TokenAuthentication {
126
+ token : ApiToken ,
127
+ user : User ,
128
+ }
129
+
130
+ impl Authentication {
120
131
pub fn user_id ( & self ) -> i32 {
121
- self . user . id
132
+ self . user ( ) . id
122
133
}
123
134
124
135
pub fn api_token_id ( & self ) -> Option < i32 > {
125
136
self . api_token ( ) . map ( |token| token. id )
126
137
}
127
138
128
139
pub fn api_token ( & self ) -> Option < & ApiToken > {
129
- self . token . as_ref ( )
140
+ match self {
141
+ Authentication :: Token ( token) => Some ( & token. token ) ,
142
+ _ => None ,
143
+ }
130
144
}
131
145
132
146
pub fn user ( & self ) -> & User {
133
- & self . user
147
+ match self {
148
+ Authentication :: Cookie ( cookie) => & cookie. user ,
149
+ Authentication :: Token ( token) => & token. user ,
150
+ }
134
151
}
135
152
}
136
153
137
- fn authenticate_user < B > ( req : & Request < B > ) -> AppResult < AuthenticatedUser > {
154
+ fn authenticate_user < B > ( req : & Request < B > ) -> AppResult < Authentication > {
138
155
controllers:: util:: verify_origin ( req) ?;
139
156
140
157
let conn = req. app ( ) . db_write ( ) ?;
@@ -151,7 +168,7 @@ fn authenticate_user<B>(req: &Request<B>) -> AppResult<AuthenticatedUser> {
151
168
152
169
req. add_custom_metadata ( "uid" , id) ;
153
170
154
- return Ok ( AuthenticatedUser { user, token : None } ) ;
171
+ return Ok ( Authentication :: Cookie ( CookieAuthentication { user } ) ) ;
155
172
}
156
173
157
174
// Otherwise, look for an `Authorization` header on the request
@@ -177,10 +194,7 @@ fn authenticate_user<B>(req: &Request<B>) -> AppResult<AuthenticatedUser> {
177
194
req. add_custom_metadata ( "uid" , token. user_id ) ;
178
195
req. add_custom_metadata ( "tokenid" , token. id ) ;
179
196
180
- return Ok ( AuthenticatedUser {
181
- user,
182
- token : Some ( token) ,
183
- } ) ;
197
+ return Ok ( Authentication :: Token ( TokenAuthentication { user, token } ) ) ;
184
198
}
185
199
186
200
// Unable to authenticate the user
0 commit comments