|
| 1 | +import { errors, symbols } from '@adonisjs/auth' |
| 2 | +import { AuthClientResponse, GuardContract } from '@adonisjs/auth/types' |
| 3 | +import { HttpContext } from '@adonisjs/core/http' |
| 4 | +import { supabase } from '../../utils/supabase_util.js' |
| 5 | +import { User } from '@supabase/supabase-js' |
| 6 | +import jwt, { JwtPayload } from 'jsonwebtoken' |
| 7 | +import app from '@adonisjs/core/services/app' |
| 8 | + |
| 9 | +export class SupabaseJwtGuard implements GuardContract<CustomSupabaseUser> { |
| 10 | + #ctx: HttpContext |
| 11 | + |
| 12 | + constructor(ctx: HttpContext) { |
| 13 | + this.#ctx = ctx |
| 14 | + } |
| 15 | + |
| 16 | + declare [symbols.GUARD_KNOWN_EVENTS]: {} |
| 17 | + |
| 18 | + driverName: 'supabase' = 'supabase' |
| 19 | + |
| 20 | + authenticationAttempted: boolean = false |
| 21 | + |
| 22 | + isAuthenticated: boolean = false |
| 23 | + |
| 24 | + user?: CustomSupabaseUser |
| 25 | + |
| 26 | + /** |
| 27 | + * Authenticate the current HTTP request and return |
| 28 | + * the user instance if there is a valid JWT token |
| 29 | + * or throw an exception |
| 30 | + */ |
| 31 | + async authenticate(): Promise<CustomSupabaseUser> { |
| 32 | + if (app.inTest) { |
| 33 | + //Skip authentication process due to testing |
| 34 | + return await this.testingAuthenticate() |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Avoid re-authentication when it has been done already |
| 39 | + * for the given request |
| 40 | + */ |
| 41 | + if (this.authenticationAttempted) { |
| 42 | + return this.getUserOrFail() |
| 43 | + } |
| 44 | + this.authenticationAttempted = true |
| 45 | + |
| 46 | + /** |
| 47 | + * Ensure the auth header exists |
| 48 | + */ |
| 49 | + const authHeader = this.#ctx.request.header('authorization') |
| 50 | + if (!authHeader) { |
| 51 | + throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', { |
| 52 | + guardDriverName: this.driverName, |
| 53 | + }) |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Split the header value and read the token from it |
| 58 | + */ |
| 59 | + const [, token] = authHeader.split('Bearer ') |
| 60 | + if (!token) { |
| 61 | + throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', { |
| 62 | + guardDriverName: this.driverName, |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get user data from supabase |
| 68 | + */ |
| 69 | + const { |
| 70 | + data: { user }, |
| 71 | + } = await supabase.auth.getUser(token) |
| 72 | + |
| 73 | + if (!user) { |
| 74 | + throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', { |
| 75 | + guardDriverName: this.driverName, |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + this.user = await this.buildCustomSupabaseUser(token, user) |
| 80 | + return this.getUserOrFail() |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Same as authenticate, but does not throw an exception |
| 85 | + */ |
| 86 | + async check(): Promise<boolean> { |
| 87 | + try { |
| 88 | + await this.authenticate() |
| 89 | + return true |
| 90 | + } catch { |
| 91 | + return false |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns the authenticated user or throws an error |
| 97 | + */ |
| 98 | + getUserOrFail(): CustomSupabaseUser { |
| 99 | + if (!this.user) { |
| 100 | + throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', { |
| 101 | + guardDriverName: this.driverName, |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | + return this.user |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * This method is called by Japa during testing when "loginAs" |
| 110 | + * method is used to login the user. |
| 111 | + */ |
| 112 | + async authenticateAsClient(user: CustomSupabaseUser): Promise<AuthClientResponse> { |
| 113 | + return { |
| 114 | + headers: { |
| 115 | + testingUser: JSON.stringify(user), |
| 116 | + }, |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + async buildCustomSupabaseUser(token: string, user: User): Promise<CustomSupabaseUser> { |
| 121 | + const customPayload = jwt.decode(token) as CustomSupabaseJwtPayload |
| 122 | + |
| 123 | + return { |
| 124 | + ...user, |
| 125 | + details: customPayload.details, |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + async testingAuthenticate(): Promise<CustomSupabaseUser> { |
| 130 | + const testingUser = this.#ctx.request.header('testingUser') |
| 131 | + if (!testingUser) { |
| 132 | + throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', { |
| 133 | + guardDriverName: this.driverName, |
| 134 | + }) |
| 135 | + } |
| 136 | + |
| 137 | + this.authenticationAttempted = true |
| 138 | + |
| 139 | + const decodedUser = JSON.parse(testingUser) |
| 140 | + this.user = { |
| 141 | + ...decodedUser, |
| 142 | + } |
| 143 | + return this.getUserOrFail() |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +export interface CustomSupabaseUser extends User { |
| 148 | + details: CustomSupabaseUserDetails |
| 149 | + // TODO: add your local profile extension if needed |
| 150 | + // e.g. profile: Profile |
| 151 | +} |
| 152 | + |
| 153 | +export interface CustomSupabaseUserDetails { |
| 154 | + // TODO: Add here your custom details |
| 155 | + // e.g. role: string | null |
| 156 | +} |
| 157 | + |
| 158 | +export interface CustomSupabaseJwtPayload extends JwtPayload { |
| 159 | + details: CustomSupabaseUserDetails |
| 160 | +} |
0 commit comments