diff --git a/package.json b/package.json index 35e3474..1e67d63 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,15 @@ { "name": "adonis-ally-azure-ad", "version": "0.3.0", - "description": "Azure AD ally driver for AdonisJS ally", + "description": "Azure AD ally driver for AdonisJS Ally", "main": "build/providers/index.js", "files": [ - "build/adonis-typings", - "build/instructions.md", "build/src", - "build/providers" + "build/providers", + "build/standalone.js", + "build/standalone.d.ts", + "build/instructions.md" ], - "types": "build/providers/index.d.ts", "scripts": { "clean": "del-cli build", "pretest": "npm run lint", @@ -60,6 +60,7 @@ "anyBranch": false }, "peerDependencies": { + "@adonisjs/ally": "^4.0.0", "@adonisjs/core": "^5.0.0" }, "adonisjs": { diff --git a/providers/index.ts b/providers/index.ts index 5722388..36cb06d 100644 --- a/providers/index.ts +++ b/providers/index.ts @@ -1,14 +1,14 @@ import type { ApplicationContract } from '@ioc:Adonis/Core/Application' -export default class AADProvider { +export default class AZureADProvider { constructor(protected app: ApplicationContract) {} public async boot() { const Ally = this.app.container.resolveBinding('Adonis/Addons/Ally') - const { AAD } = await import('../src/AzureAD') + const { AZureADDriver } = await import('../src/AzureAD') - Ally.extend('AzureAD', (_, __, config, ctx) => { - return new AAD(ctx, config) + Ally.extend('azuread', (_, __, config, ctx) => { + return new AZureADDriver(ctx, config) }) } } diff --git a/src/AzureAD/index.ts b/src/AzureAD/index.ts index e6aed36..00554f2 100644 --- a/src/AzureAD/index.ts +++ b/src/AzureAD/index.ts @@ -10,17 +10,72 @@ | */ -import type { AllyUserContract, ApiRequestContract } from '@ioc:Adonis/Addons/Ally' -import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' +import { ApiRequest, Oauth2Driver, RedirectRequest } from '@adonisjs/ally/build/standalone' import type { - AADAccessToken, - AADConfig, - AADScopes, - UserFields, - UserFieldsAndToken, - UserInfo, -} from '../types' -import { Oauth2Driver, ApiRequest, RedirectRequest } from '@adonisjs/ally/build/standalone' + AllyUserContract, + ApiRequestContract, + LiteralStringUnion, +} from '@ioc:Adonis/Addons/Ally' +import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' + +export type AzureADAccessToken = { + token: string + type: string + token_type: string + scope: string + expires_in: number + ext_expires_in: number + access_token: string + refresh_token: string + id_token: string +} + +export type AZureADScopes = string + +export type AZureADConfig = { + driver: 'azuread' + clientId: string + clientSecret: string + callbackUrl: string + authorizeUrl?: string + accessTokenUrl?: string + userInfoUrl?: string + scopes?: LiteralStringUnion[] +} + +export type UserInfo = { + '@odata.context': string + '@odata.id': string + 'businessPhones': string[] + 'displayName': string + 'givenName': string + 'jobTitle': string + 'mail': string + 'mobilePhone': string + 'officeLocation': string + 'preferredLanguage'?: any + 'surname': string + 'userPrincipalName': string + 'id': string +} + +export type UserFields = { + id: string + avatarUrl: string | null + nickName: string + displayName?: string | undefined + name: string + email: string | null + emailVerificationState: 'verified' | 'unverified' | 'unsupported' + original: UserInfo | null +} + +export interface UserFieldsAndToken extends UserFields { + token: { + token: string + type: 'bearer' + } +} /** * Driver implementation. It is mostly configuration driven except the user calls @@ -29,7 +84,7 @@ import { Oauth2Driver, ApiRequest, RedirectRequest } from '@adonisjs/ally/build/ * Change "AAD" to something more relevant * ------------------------------------------------ */ -export class AAD extends Oauth2Driver { +export class AZureADDriver extends Oauth2Driver { /** * The URL for the authority data * @@ -96,7 +151,7 @@ export class AAD extends Oauth2Driver { */ protected scopesSeparator = ' ' - constructor(ctx: HttpContextContract, public config: AADConfig) { + constructor(ctx: HttpContextContract, public config: AZureADConfig) { super(ctx, config) config.scopes = config.scopes || ['openid', 'profile', 'email', 'offline_access'] @@ -116,7 +171,7 @@ export class AAD extends Oauth2Driver { /** * Configuring the redirect request with defaults */ - protected configureRedirectRequest(request: RedirectRequest): void { + protected configureRedirectRequest(request: RedirectRequest): void { /** * Define user defined scopes or the default one's */ @@ -200,7 +255,7 @@ export class AAD extends Oauth2Driver { */ public async user( callback?: (request: ApiRequest) => void - ): Promise> { + ): Promise> { const accessToken = await this.accessToken() /** @@ -226,7 +281,7 @@ export class AAD extends Oauth2Driver { return { ...user, - token: { token, type: 'bearer' as const }, + token: { token, type: 'bearer' }, } } } diff --git a/src/types.d.ts b/src/types.d.ts deleted file mode 100644 index 8ee6f0e..0000000 --- a/src/types.d.ts +++ /dev/null @@ -1,85 +0,0 @@ -import type { LiteralStringUnion } from '@ioc:Adonis/Addons/Ally' - -/** - * Define the access token object properties in this type. It - * must have "token" and "type" and you are free to add - * more properties. - * - * ------------------------------------------------ - * Change "AAD" to something more relevant - * ------------------------------------------------ - */ -export type AADAccessToken = { - token: string - type: string - token_type: string - scope: string - expires_in: number - ext_expires_in: number - access_token: string - refresh_token: string - id_token: string -} - -/** - * Define a union of scopes your driver accepts. Here's an example of same - * https://github.com/adonisjs/ally/blob/develop/adonis-typings/ally.ts#L236-L268 - * - * ------------------------------------------------ - * Change "AAD" to something more relevant - * ------------------------------------------------ - */ -export type AADScopes = string - -/** - * Define the configuration options accepted by your driver. It must have the following - * properties and you are free add more. - * - * ------------------------------------------------ - * Change "AAD" to something more relevant - * ------------------------------------------------ - */ -export type AADConfig = { - driver: 'AzureAD' - clientId: string - clientSecret: string - callbackUrl: string - authorizeUrl?: string - accessTokenUrl?: string - userInfoUrl?: string - scopes?: LiteralStringUnion[] -} - -export type UserInfo = { - '@odata.context': string - '@odata.id': string - 'businessPhones': string[] - 'displayName': string - 'givenName': string - 'jobTitle': string - 'mail': string - 'mobilePhone': string - 'officeLocation': string - 'preferredLanguage'?: any - 'surname': string - 'userPrincipalName': string - 'id': string -} - -export type UserFields = { - id: string - avatarUrl: string | null - nickName: string - displayName?: string | undefined - name: string - email: string | null - emailVerificationState: 'verified' | 'unverified' | 'unsupported' - original: UserInfo | null -} - -export interface UserFieldsAndToken extends UserFields { - token: { - token: string - type: const - } -}