From bc21d0d5083550892fccac037c4562b05771d701 Mon Sep 17 00:00:00 2001 From: Arthur DANJOU Date: Wed, 30 Jun 2021 18:36:04 +0200 Subject: [PATCH] Working on OAuth --- config/ally.ts | 6 +- config/auth.ts | 63 +------------------ contracts/auth.ts | 13 ---- ..._tokens.ts => 1625068552541_api_tokens.ts} | 4 -- env.ts | 6 +- start/routes.ts | 43 ++++++------- 6 files changed, 27 insertions(+), 108 deletions(-) rename database/migrations/{1605093087010_api_tokens.ts => 1625068552541_api_tokens.ts} (88%) diff --git a/config/ally.ts b/config/ally.ts index 19ad0aa..42279d4 100644 --- a/config/ally.ts +++ b/config/ally.ts @@ -27,7 +27,7 @@ const allyConfig: AllyConfig = { driver: 'github', clientId: Env.get('GITHUB_CLIENT_ID'), clientSecret: Env.get('GITHUB_CLIENT_SECRET'), - callbackUrl: `${Env.get('HOST')}:${Env.get('PORT')}/auth/github`, + callbackUrl: `${Env.get('BASE_URL')}:${Env.get('PORT')}/auth/github/callback`, }, /* |-------------------------------------------------------------------------- @@ -38,7 +38,7 @@ const allyConfig: AllyConfig = { driver: 'twitter', clientId: Env.get('TWITTER_CLIENT_ID'), clientSecret: Env.get('TWITTER_CLIENT_SECRET'), - callbackUrl: `${Env.get('HOST')}:${Env.get('PORT')}/auth/twitter`, + callbackUrl: `${Env.get('BASE_URL')}:${Env.get('PORT')}/auth/twitter/callback`, }, /* |-------------------------------------------------------------------------- @@ -49,7 +49,7 @@ const allyConfig: AllyConfig = { driver: 'google', clientId: Env.get('GOOGLE_CLIENT_ID'), clientSecret: Env.get('GOOGLE_CLIENT_SECRET'), - callbackUrl: `${Env.get('HOST')}:${Env.get('PORT')}/auth/google`, + callbackUrl: `${Env.get('BASE_URL')}:${Env.get('PORT')}/auth/google/callback`, }, } diff --git a/config/auth.ts b/config/auth.ts index 557179b..1209c01 100644 --- a/config/auth.ts +++ b/config/auth.ts @@ -17,69 +17,8 @@ import { AuthConfig } from '@ioc:Adonis/Addons/Auth' | */ const authConfig: AuthConfig = { - guard: 'web', + guard: 'api', guards: { - /* - |-------------------------------------------------------------------------- - | Web Guard - |-------------------------------------------------------------------------- - | - | Web guard uses classic old school sessions for authenticating users. - | If you are building a standard web application, it is recommended to - | use web guard with session driver - | - */ - web: { - driver: 'session', - - provider: { - /* - |-------------------------------------------------------------------------- - | Driver - |-------------------------------------------------------------------------- - | - | Name of the driver - | - */ - driver: 'lucid', - - /* - |-------------------------------------------------------------------------- - | Identifier key - |-------------------------------------------------------------------------- - | - | The identifier key is the unique key on the model. In most cases specifying - | the primary key is the right choice. - | - */ - identifierKey: 'id', - - /* - |-------------------------------------------------------------------------- - | Uids - |-------------------------------------------------------------------------- - | - | Uids are used to search a user against one of the mentioned columns. During - | login, the auth module will search the user mentioned value against one - | of the mentioned columns to find their user record. - | - */ - uids: ['email'], - - /* - |-------------------------------------------------------------------------- - | Model - |-------------------------------------------------------------------------- - | - | The model to use for fetching or finding users. The model is imported - | lazily since the config files are read way earlier in the lifecycle - | of booting the app and the models may not be in a usable state at - | that time. - | - */ - model: () => import('App/Models/User'), - }, - }, /* |-------------------------------------------------------------------------- | OAT Guard diff --git a/contracts/auth.ts b/contracts/auth.ts index a498628..90916e2 100644 --- a/contracts/auth.ts +++ b/contracts/auth.ts @@ -55,19 +55,6 @@ declare module '@ioc:Adonis/Addons/Auth' { | */ interface GuardsList { - /* - |-------------------------------------------------------------------------- - | Web Guard - |-------------------------------------------------------------------------- - | - | The web guard uses sessions for maintaining user login state. It uses - | the `user` provider for fetching user details. - | - */ - web: { - implementation: SessionGuardContract<'user', 'web'>, - config: SessionGuardConfig<'user'>, - }, /* |-------------------------------------------------------------------------- | OAT Guard diff --git a/database/migrations/1605093087010_api_tokens.ts b/database/migrations/1625068552541_api_tokens.ts similarity index 88% rename from database/migrations/1605093087010_api_tokens.ts rename to database/migrations/1625068552541_api_tokens.ts index 1cf309c..ed8d363 100644 --- a/database/migrations/1605093087010_api_tokens.ts +++ b/database/migrations/1625068552541_api_tokens.ts @@ -10,10 +10,6 @@ export default class ApiTokens extends BaseSchema { table.string('name').notNullable() table.string('type').notNullable() table.string('token', 64).notNullable() - - /** - * "useTz: true" utilizes timezone option in PostgreSQL and MSSQL - */ table.timestamp('expires_at', { useTz: true }).nullable() table.timestamp('created_at', { useTz: true }).notNullable() }) diff --git a/env.ts b/env.ts index d4e8801..8f2d2db 100644 --- a/env.ts +++ b/env.ts @@ -17,7 +17,7 @@ import Env from '@ioc:Adonis/Core/Env' export default Env.rules({ //App - HOST: Env.schema.string({ format: 'host' }), + HOST: Env.schema.string(), PORT: Env.schema.number(), APP_KEY: Env.schema.string(), APP_NAME: Env.schema.string(), @@ -32,7 +32,7 @@ export default Env.rules({ //Mysql DB_CONNECTION: Env.schema.string(), - MYSQL_HOST: Env.schema.string({ format: 'host' }), + MYSQL_HOST: Env.schema.string(), MYSQL_PORT: Env.schema.number(), MYSQL_USER: Env.schema.string(), MYSQL_PASSWORD: Env.schema.string.optional(), @@ -47,7 +47,7 @@ export default Env.rules({ //Utils GITHUB_TOKEN: Env.schema.string(), GITHUB_SOURCE: Env.schema.string({ format: 'url' }), - BASE_URL: Env.schema.string({ format: 'url' }), + BASE_URL: Env.schema.string(), API_VERSION: Env.schema.string(), //Mails diff --git a/start/routes.ts b/start/routes.ts index 66e4327..6fc02cb 100644 --- a/start/routes.ts +++ b/start/routes.ts @@ -26,7 +26,7 @@ Route.get('/source', async ({response}: HttpContextContract) => { return response.redirect(Env.get('GITHUB_SOURCE')) }) -Route.get('health', async ({response}: HttpContextContract) => { +Route.get('/health', async ({response}: HttpContextContract) => { const report = await HealthCheck.getReport() const isLive = await HealthCheck.isLive() const isReady = await HealthCheck.isReady() @@ -36,31 +36,20 @@ Route.get('health', async ({response}: HttpContextContract) => { // ArtAPI Route.get('/profile', 'ProfileController.me') Route.get('/locations', 'LocationsController.get') -//Route.get('/stats', 'StatsController.get') +Route.get('/stats', 'StatsController.get') Route.get('/states', 'StatesController.get') Route.get('/projects', 'ProjectsController.get') -Route.resource('users', 'UsersController').only(['index', 'show']) - -Route.group(() => { - Route.get('/', 'FileController.index') - Route.get('/:filename', async ({ response, params }) => { - response.download(Application.makePath('storage', params.filename)) - }) -}).prefix('/files') - -Route.group(() => { - Route.resource('users', 'UsersController').only(['store', 'update', 'destroy']) - Route.resource('files', 'FileController').only(['store', 'destroy']) - Route.post('/locations', 'LocationsController.store') - Route.post('/projects', 'ProjectsController.store') -}).middleware('auth:web') - Route.group(() => { Route.post('form', 'FormsController.send') - Route.post('states/:state', 'StatesController.set') + Route.resource('users', 'UsersController') + Route.resource('files', 'FileController').only(['store', 'destroy']) + + Route.post('/locations', 'LocationsController.store') + Route.post('/projects', 'ProjectsController.store') + Route.group(() => { Route.get('/:slug', 'PostsController.getLikes') Route.post('/:slug/like', 'PostsController.like') @@ -72,7 +61,15 @@ Route.group(() => { Route.get('guestbook', 'GuestBookController.get') Route.post('guestbook', 'GuestBookController.store') -}).middleware('auth:api') + + Route.group(() => { + Route.get('/', 'FileController.index') + Route.get('/:filename', async ({ response, params }) => { + response.download(Application.makePath('storage', params.filename)) + }) + }).prefix('/files') + +}).middleware('auth') Route.group(() => { Route.get('/me', 'AuthController.user').middleware('auth') @@ -88,13 +85,13 @@ Route.group(() => { Route.get('/github/callback', 'AuthController.github') Route.get('/google/callback', 'AuthController.google') - Route.get('/twitter/redirect', async ({ ally}) => { + Route.get('/twitter', async ({ ally }) => { return ally.use('twitter').redirect() }) - Route.get('/github/redirect', async ({ ally}) => { + Route.get('/github', async ({ ally }) => { return ally.use('github').redirect() }) - Route.get('/google/redirect', async ({ ally}) => { + Route.get('/google', async ({ ally }) => { return ally.use('google').redirect() }) }).prefix('auth')