Skip to content

Commit

Permalink
feat: finish base implementations and session guard
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 23, 2023
1 parent 317601f commit 564987d
Show file tree
Hide file tree
Showing 37 changed files with 678 additions and 307 deletions.
4 changes: 4 additions & 0 deletions bin/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ configure({
name: 'session',
files: ['tests/guards/session/**/*.spec.ts'],
},
{
name: 'auth',
files: ['tests/auth/**/*.spec.ts'],
},
{
name: 'core',
files: ['tests/core/**/*.spec.ts'],
Expand Down
4 changes: 2 additions & 2 deletions factories/database_user_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import { Hash } from '@adonisjs/core/hash'
import type { Database } from '@adonisjs/lucid/database'
import { Scrypt } from '@adonisjs/core/hash/drivers/scrypt'
import { DatabaseUserProvider } from '../src/core/user_providers/database.js'
import { BaseDatabaseUserProvider } from '../src/core/user_providers/database.js'

export class TestDatabaseUserProvider<
RealUser extends Record<string, any>,
> extends DatabaseUserProvider<RealUser> {}
> extends BaseDatabaseUserProvider<RealUser> {}

/**
* Creates an instance of the DatabaseUserProvider with sane
Expand Down
36 changes: 17 additions & 19 deletions factories/lucid_user_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import { Hash } from '@adonisjs/core/hash'
import { BaseModel, column } from '@adonisjs/lucid/orm'
import { Scrypt } from '@adonisjs/core/hash/drivers/scrypt'
import { LucidUserProvider } from '../src/core/user_providers/lucid.js'
import { LucidAuthenticatable, LucidUserProviderOptions } from '../src/core/types.js'
import { PROVIDER_REAL_USER } from '../src/symbols.js'

import { PROVIDER_REAL_USER } from '../src/auth/symbols.js'
import { BaseLucidUserProvider } from '../src/core/user_providers/lucid.js'
import type { LucidAuthenticatable, LucidUserProviderOptions } from '../src/core/types.js'

export class FactoryUser extends BaseModel {
static table = 'users'
Expand Down Expand Up @@ -49,7 +50,7 @@ export class FactoryUser extends BaseModel {

export class TestLucidUserProvider<
UserModel extends LucidAuthenticatable,
> extends LucidUserProvider<UserModel> {
> extends BaseLucidUserProvider<UserModel> {
declare [PROVIDER_REAL_USER]: InstanceType<UserModel>
}

Expand All @@ -58,23 +59,20 @@ export class TestLucidUserProvider<
* defaults for testing
*/
export class LucidUserProviderFactory {
createForModel<Model extends LucidAuthenticatable>(
model: Model,
options: LucidUserProviderOptions<Model>
) {
return new TestLucidUserProvider(
async () => {
return {
default: model,
}
},
{
...options,
}
)
createForModel<Model extends LucidAuthenticatable>(options: LucidUserProviderOptions<Model>) {
return new TestLucidUserProvider({
...options,
})
}

create() {
return this.createForModel(FactoryUser, { uids: ['email', 'username'] })
return this.createForModel({
model: async () => {
return {
default: FactoryUser,
}
},
uids: ['email', 'username'],
})
}
}
7 changes: 5 additions & 2 deletions factories/session_guard_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

import type { HttpContext } from '@adonisjs/core/http'

import { SessionGuard } from '../src/session/guard.js'
import type { SessionGuardConfig, SessionUserProviderContract } from '../src/session/types.js'
import { SessionGuard } from '../src/guards/session/guard.js'
import type {
SessionGuardConfig,
SessionUserProviderContract,
} from '../src/guards/session/types.js'
import {
FactoryUser,
TestLucidUserProvider,
Expand Down
8 changes: 5 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* file that was distributed with this source code.
*/

export * as errors from './src/errors.js'
export * as symbols from './src/symbols.js'
export { Authenticator } from './src/authenticator.js'
export * as errors from './src/auth/errors.js'
export * as symbols from './src/auth/symbols.js'
export { Authenticator } from './src/auth/authenticator.js'
export { AuthManager } from './src/auth/auth_manager.js'
export { defineConfig, providers } from './src/auth/define_config.js'
38 changes: 19 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
"exports": {
".": "./build/index.js",
"./types": "./build/src/types/main.js",

"./core/token": "./build/src/core/token.js",
"./core/guard_user": "./build/src/core/guard_user.js",
"./core/user_providers/*": "./build/src/core/user_providers/*.js",
"./core/token_providers/*": "./build/src/core/token_providers/*.js",
"./session": "./build/src/session/main.js",
"./session/user_providers": "./build/src/session/user_providers/main.js",
"./session/token_providers": "./build/src/session/token_providers/main.js",
"./types/session": "./build/src/session/types.js",
"./types/core": "./build/src/core/types.js"
"./types/core": "./build/src/core/types.js",

"./session": "./build/src/guards/session/main.js",
"./types/session": "./build/src/guards/session/types.js"
},
"scripts": {
"pretest": "npm run lint",
Expand Down Expand Up @@ -65,28 +65,28 @@
"url": "https://github.com/adonisjs/auth/issues"
},
"devDependencies": {
"@adonisjs/core": "^6.1.5-26",
"@adonisjs/core": "^6.1.5-30",
"@adonisjs/eslint-config": "^1.1.8",
"@adonisjs/lucid": "^19.0.0-2",
"@adonisjs/lucid": "^19.0.0-3",
"@adonisjs/prettier-config": "^1.1.8",
"@adonisjs/session": "^7.0.0-11",
"@adonisjs/session": "^7.0.0-13",
"@adonisjs/tsconfig": "^1.1.8",
"@commitlint/cli": "^17.7.2",
"@commitlint/config-conventional": "^17.7.0",
"@japa/assert": "^2.0.0-2",
"@japa/expect-type": "^2.0.0-1",
"@japa/file-system": "^2.0.0-2",
"@japa/runner": "^3.0.1",
"@commitlint/cli": "^18.0.0",
"@commitlint/config-conventional": "^18.0.0",
"@japa/assert": "^2.0.0",
"@japa/expect-type": "^2.0.0",
"@japa/file-system": "^2.0.0",
"@japa/runner": "^3.0.4",
"@japa/snapshot": "^2.0.0",
"@swc/core": "1.3.82",
"@types/luxon": "^3.3.2",
"@types/node": "^20.8.3",
"@types/set-cookie-parser": "^2.4.4",
"@types/luxon": "^3.3.3",
"@types/node": "^20.8.7",
"@types/set-cookie-parser": "^2.4.5",
"c8": "^8.0.1",
"copyfiles": "^2.4.1",
"cross-env": "^7.0.3",
"del-cli": "^5.1.0",
"eslint": "^8.25.0",
"eslint": "^8.52.0",
"github-label-sync": "^2.3.1",
"husky": "^8.0.3",
"luxon": "^3.4.3",
Expand Down Expand Up @@ -128,7 +128,7 @@
]
},
"dependencies": {
"@poppinss/utils": "^6.5.0-7"
"@poppinss/utils": "^6.5.0"
},
"peerDependencies": {
"@adonisjs/core": "^6.1.5-26",
Expand Down
40 changes: 40 additions & 0 deletions providers/auth_provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* @adonisjs/auth
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { configProvider } from '@adonisjs/core'
import { RuntimeException } from '@poppinss/utils'
import type { ApplicationService } from '@adonisjs/core/types'

import { AuthManager } from '../src/auth/auth_manager.js'
import type { AuthService } from '../src/auth/types.js'

declare module '@adonisjs/core/types' {
export interface ContainerBindings {
'auth.manager': AuthService
}
}

export default class AuthProvider {
constructor(protected app: ApplicationService) {}

register() {
this.app.container.singleton('auth.manager', async () => {
const authConfigProvider = this.app.config.get('auth')
const config = await configProvider.resolve<any>(this.app, authConfigProvider)

if (!config) {
throw new RuntimeException(
'Invalid config exported from "config/auth.ts" file. Make sure to use the defineConfig method'
)
}

return new AuthManager(config)
})
}
}
22 changes: 22 additions & 0 deletions services/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* @adonisjs/auth
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import app from '@adonisjs/core/services/app'
import { AuthService } from '../src/auth/types.js'

let auth: AuthService

/**
* Returns a singleton instance of the Auth manager class
*/
await app.booted(async () => {
auth = await app.container.make('auth.manager')
})

export { auth as default }
8 changes: 4 additions & 4 deletions src/auth_manager.ts → src/auth/auth_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@

import type { HttpContext } from '@adonisjs/core/http'

import type { GuardFactory } from './types.js'
import { Authenticator } from './authenticator.js'
import type { AuthenticatorGuardFactory } from './types/main.js'

/**
* Auth manager exposes the API to register and manage authentication
* guards from the config
*/
export class AuthManager<KnownGuards extends Record<string, AuthenticatorGuardFactory>> {
export class AuthManager<KnownGuards extends Record<string, GuardFactory>> {
/**
* Registered guards
*/
#config: {
default?: keyof KnownGuards
default: keyof KnownGuards
guards: KnownGuards
}

constructor(config: { default?: keyof KnownGuards; guards: KnownGuards }) {
constructor(config: { default: keyof KnownGuards; guards: KnownGuards }) {
this.#config = config
}

Expand Down
22 changes: 12 additions & 10 deletions src/authenticator.ts → src/auth/authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import type { HttpContext } from '@adonisjs/core/http'

import debug from './debug.js'
import type { AuthenticatorGuardFactory } from './types/main.js'
import type { GuardFactory } from './types.js'

/**
* Authenticator is an HTTP request specific implementation for using
* guards to login users and authenticate requests.
*/
export class Authenticator<KnownGuards extends Record<string, AuthenticatorGuardFactory>> {
export class Authenticator<KnownGuards extends Record<string, GuardFactory>> {
/**
* Reference to HTTP context
*/
Expand All @@ -26,7 +26,7 @@ export class Authenticator<KnownGuards extends Record<string, AuthenticatorGuard
* Registered guards
*/
#config: {
default?: keyof KnownGuards
default: keyof KnownGuards
guards: KnownGuards
}

Expand All @@ -35,7 +35,7 @@ export class Authenticator<KnownGuards extends Record<string, AuthenticatorGuard
*/
#guardsCache: Partial<Record<keyof KnownGuards, unknown>> = {}

constructor(ctx: HttpContext, config: { default?: keyof KnownGuards; guards: KnownGuards }) {
constructor(ctx: HttpContext, config: { default: keyof KnownGuards; guards: KnownGuards }) {
this.#ctx = ctx
this.#config = config
debug('creating authenticator. config %O', this.#config)
Expand All @@ -45,24 +45,26 @@ export class Authenticator<KnownGuards extends Record<string, AuthenticatorGuard
* Returns an instance of a known guard. Guards instances are
* cached during the lifecycle of an HTTP request.
*/
use<Guard extends keyof KnownGuards>(guard: Guard): ReturnType<KnownGuards[Guard]> {
use<Guard extends keyof KnownGuards>(guard?: Guard): ReturnType<KnownGuards[Guard]> {
const guardToUse = guard || this.#config.default

/**
* Use cached copy if exists
*/
const cachedGuard = this.#guardsCache[guard]
const cachedGuard = this.#guardsCache[guardToUse]
if (cachedGuard) {
debug('using guard from cache. name: "%s"', guard)
debug('using guard from cache. name: "%s"', guardToUse)
return cachedGuard as ReturnType<KnownGuards[Guard]>
}

const guardFactory = this.#config.guards[guard]
const guardFactory = this.#config.guards[guardToUse]

/**
* Construct guard and cache it
*/
debug('creating guard. name: "%s"', guard)
debug('creating guard. name: "%s"', guardToUse)
const guardInstance = guardFactory(this.#ctx)
this.#guardsCache[guard] = guardInstance
this.#guardsCache[guardToUse] = guardInstance

return guardInstance as ReturnType<KnownGuards[Guard]>
}
Expand Down
File renamed without changes.
Loading

0 comments on commit 564987d

Please sign in to comment.