This repository has been archived by the owner on Jul 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update auth context and implement cookie renewal
- Loading branch information
1 parent
7ce5f2c
commit ee40367
Showing
43 changed files
with
472 additions
and
273 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Authentication | ||
|
||
The backend authenticates requests using signed cookies so they can contain user information so that it does not have to be fetched for every request. | ||
|
||
The cookie contains the user's id. | ||
|
||
Cookies are sent [`secure` and `HttpOnly`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) when users register their account, or when they login using username and password. | ||
|
||
Cookies expire after 30 minutes and the client is responsible to renew cookies by calling the `GET /me/cookie` endpoint before they expire. | ||
|
||
When renewing cookies the server will re-check if the user still exists and if they haven't changed their password. For this a hash of the user's password hash, email, username, and id will be generated and included in the cookie. If any of these properties changes, the cookie cannot be renewed and the user has to log-in again. | ||
|
||
## Admin permissions | ||
|
||
Admin permission are granted via the `isAdmin` flag on the `UserAccount`. | ||
|
||
## Configuration | ||
|
||
These environment variables control the authentication: | ||
|
||
- `COOKIE_SECRET`: sets the secret used to sign cookies, default value is a random string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,77 @@ | ||
import * as crypto from 'crypto' | ||
import { CookieOptions } from 'express' | ||
import { Strategy as CookieStrategy } from 'passport-cookie' | ||
import UserAccount from './models/user_account' | ||
|
||
const fakeAccount = UserAccount.build({ | ||
username: '', | ||
token: '', | ||
passwordHash: '', | ||
}) | ||
type AuthCookiePayload = { | ||
/** user ID */ | ||
i: number | ||
/** user is admin */ | ||
a: boolean | ||
/** user hash */ | ||
c: string | ||
} | ||
|
||
export type AuthContext = { | ||
userAccount: UserAccount | ||
userId: number | ||
isAdmin: boolean | ||
userHash: string | ||
} | ||
|
||
export type ErrorInfo = { | ||
message: string | ||
} | ||
|
||
export const fakeAdminAuth: AuthContext = { | ||
userAccount: fakeAccount, | ||
isAdmin: true, | ||
} | ||
|
||
export const fakeUserAuth: AuthContext = { | ||
userAccount: fakeAccount, | ||
isAdmin: false, | ||
} | ||
|
||
export const authenticateWithToken = async ( | ||
token: string, | ||
): Promise<AuthContext | ErrorInfo> => { | ||
try { | ||
const userAccount = await UserAccount.findOne({ | ||
where: { token }, | ||
}) | ||
if (userAccount === null) return { message: 'User not found for token.' } | ||
return { userAccount, isAdmin: false } | ||
} catch (err) { | ||
return err | ||
} | ||
} | ||
export const userHash = (user: UserAccount): string => | ||
crypto | ||
.createHash('sha1') | ||
.update(`${user.id}:${user.username}:${user.passwordHash}`) | ||
.digest('hex') | ||
|
||
export const authTokenCookieName = 'token' | ||
export const authCookieName = 'auth' | ||
export const cookieAuthStrategy = new CookieStrategy( | ||
{ | ||
cookieName: authTokenCookieName, | ||
cookieName: authCookieName, | ||
signed: true, | ||
}, | ||
async (token: string, done: any) => { | ||
const res = await authenticateWithToken(token) | ||
if ('userAccount' in res) return done(null, res) | ||
return done(null, false, res) | ||
async (value: string, done: any) => { | ||
try { | ||
return done(null, decodeAuthCookie(value)) | ||
} catch (error) { | ||
return done( | ||
null, | ||
false, | ||
new Error(`Failed to decode cookie payload: ${error.message}!`), | ||
) | ||
} | ||
}, | ||
) | ||
|
||
export const authCookie = ( | ||
user: UserAccount, | ||
lifetimeInMinutes: number = 30, | ||
): [string, string, CookieOptions] => [ | ||
authCookieName, | ||
JSON.stringify({ | ||
i: user.id, | ||
a: false, | ||
c: userHash(user), | ||
}), | ||
{ | ||
signed: true, | ||
secure: true, | ||
httpOnly: true, | ||
expires: new Date(Date.now() + lifetimeInMinutes * 60 * 1000), | ||
}, | ||
] | ||
|
||
export const userToAuthContext = (user: UserAccount): AuthContext => ({ | ||
isAdmin: user.isAdmin, | ||
userId: user.id, | ||
userHash: userHash(user), | ||
}) | ||
|
||
export const decodeAuthCookie = (value: string): AuthContext => { | ||
const { | ||
i: userId, | ||
a: isAdmin, | ||
c: userHash, | ||
} = JSON.parse(value) as AuthCookiePayload | ||
return { userId, isAdmin, userHash } | ||
} |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const trimAll = (o: Record<string, string>): Record<string, string> => | ||
Object.entries(o).reduce( | ||
(r, [k, v]) => ({ | ||
...r, | ||
[k]: v.trim(), | ||
}), | ||
{}, | ||
) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.