Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![CI](https://github.com/fastify/fastify-oauth2/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/fastify-oauth2/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/@fastify/oauth2.svg?style=flat)](https://www.npmjs.com/package/@fastify/oauth2)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

Wrapper around the [`simple-oauth2`](https://github.com/lelylan/simple-oauth2) library.

Expand Down
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict'

module.exports = require('neostandard')({
ignores: require('neostandard').resolveIgnoresFromGitignore(),
ts: true
})
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"type": "commonjs",
"types": "types/index.d.ts",
"scripts": {
"lint": "standard | snazzy",
"lint:fix": "standard --fix",
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:coverage": "npm run test:unit -- --cov --coverage-report=html",
"test:typescript": "tsd",
Expand All @@ -32,10 +32,9 @@
"@types/node": "^22.0.0",
"@types/simple-oauth2": "^5.0.7",
"fastify": "^5.0.0",
"neostandard": "^0.12.0",
"nock": "^13.5.4",
"simple-get": "^4.0.1",
"snazzy": "^9.0.0",
"standard": "^17.1.0",
"tap": "^18.7.1",
"tsd": "^0.31.0"
},
Expand Down
302 changes: 151 additions & 151 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { FastifyPluginCallback, FastifyReply, FastifyRequest, FastifyInstance } from 'fastify';
import { CookieSerializeOptions } from "@fastify/cookie";
import { FastifyPluginCallback, FastifyReply, FastifyRequest, FastifyInstance } from 'fastify'
import { CookieSerializeOptions } from '@fastify/cookie'

interface FastifyOauth2 extends FastifyPluginCallback<fastifyOauth2.FastifyOAuth2Options> {
APPLE_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
DISCORD_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
FACEBOOK_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
GITHUB_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
GITLAB_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
LINKEDIN_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
GOOGLE_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
MICROSOFT_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
SPOTIFY_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
VKONTAKTE_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
TWITCH_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
VATSIM_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
VATSIM_DEV_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
EPIC_GAMES_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
YANDEX_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
APPLE_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
DISCORD_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
FACEBOOK_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
GITHUB_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
GITLAB_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
LINKEDIN_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
GOOGLE_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
MICROSOFT_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
SPOTIFY_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
VKONTAKTE_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
TWITCH_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
VATSIM_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
VATSIM_DEV_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
EPIC_GAMES_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
YANDEX_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
}

declare namespace fastifyOauth2 {
Expand Down Expand Up @@ -50,163 +50,163 @@ declare namespace fastifyOauth2 {
verifierCookieName?: string;
}

export type TToken = 'access_token' | 'refresh_token'
export type TToken = 'access_token' | 'refresh_token'

export interface Token {
token_type: 'Bearer';
access_token: string;
refresh_token?: string;
id_token?: string;
expires_in: number;
expires_at: Date;
}
export interface Token {
token_type: 'Bearer';
access_token: string;
refresh_token?: string;
id_token?: string;
expires_in: number;
expires_at: Date;
}

export interface OAuth2Token {
/**
export interface OAuth2Token {
/**
* Immutable object containing the token object provided while constructing a new access token instance.
* This property will usually have the schema as specified by RFC6750,
* but the exact properties may vary between authorization servers.
*/
token: Token;
token: Token;

/**
/**
* Determines if the current access token is definitely expired or not
* @param expirationWindowSeconds Window of time before the actual expiration to refresh the token. Defaults to 0.
*/
expired(expirationWindowSeconds?: number): boolean;

/** Refresh the access token */
refresh(params?: {}): Promise<OAuth2Token>;

/** Revoke access or refresh token */
revoke(tokenType: 'access_token' | 'refresh_token'): Promise<void>;

/** Revoke both the existing access and refresh tokens */
revokeAll(): Promise<void>;
}

export interface ProviderConfiguration {
/** String used to set the host to request the tokens to. Required. */
tokenHost: string;
/** String path to request an access token. Default to /oauth/token. */
tokenPath?: string | undefined;
/** String path to revoke an access token. Default to /oauth/revoke. */
revokePath?: string | undefined;
/** String used to set the host to request an "authorization code". Default to the value set on auth.tokenHost. */
authorizeHost?: string | undefined;
/** String path to request an authorization code. Default to /oauth/authorize. */
authorizePath?: string | undefined;
}

export interface Credentials {
client: {
/** Service registered client id. Required. */
id: string;
/** Service registered client secret. Required. */
secret: string;
/** Parameter name used to send the client secret. Default to client_secret. */
secretParamName?: string | undefined;
/** Parameter name used to send the client id. Default to client_id. */
idParamName?: string | undefined;
};
auth?: ProviderConfiguration;
/**
expired(expirationWindowSeconds?: number): boolean;

/** Refresh the access token */
refresh(params?: {}): Promise<OAuth2Token>;

/** Revoke access or refresh token */
revoke(tokenType: 'access_token' | 'refresh_token'): Promise<void>;

/** Revoke both the existing access and refresh tokens */
revokeAll(): Promise<void>;
}

export interface ProviderConfiguration {
/** String used to set the host to request the tokens to. Required. */
tokenHost: string;
/** String path to request an access token. Default to /oauth/token. */
tokenPath?: string | undefined;
/** String path to revoke an access token. Default to /oauth/revoke. */
revokePath?: string | undefined;
/** String used to set the host to request an "authorization code". Default to the value set on auth.tokenHost. */
authorizeHost?: string | undefined;
/** String path to request an authorization code. Default to /oauth/authorize. */
authorizePath?: string | undefined;
}

export interface Credentials {
client: {
/** Service registered client id. Required. */
id: string;
/** Service registered client secret. Required. */
secret: string;
/** Parameter name used to send the client secret. Default to client_secret. */
secretParamName?: string | undefined;
/** Parameter name used to send the client id. Default to client_id. */
idParamName?: string | undefined;
};
auth?: ProviderConfiguration;
/**
* Used to set global options to the internal http library (wreck).
* All options except baseUrl are allowed
* Defaults to header.Accept = "application/json"
*/
http?: {} | undefined;
options?: {
/** Format of data sent in the request body. Defaults to form. */
bodyFormat?: "json" | "form" | undefined;
/**
http?: {} | undefined;
options?: {
/** Format of data sent in the request body. Defaults to form. */
bodyFormat?: 'json' | 'form' | undefined;
/**
* Indicates the method used to send the client.id/client.secret authorization params at the token request.
* If set to body, the bodyFormat option will be used to format the credentials.
* Defaults to header
*/
authorizationMethod?: "header" | "body" | undefined;
} | undefined;
}

export interface OAuth2Namespace {
getAccessTokenFromAuthorizationCodeFlow(
request: FastifyRequest,
): Promise<OAuth2Token>;

getAccessTokenFromAuthorizationCodeFlow(
request: FastifyRequest,
reply: FastifyReply,
): Promise<OAuth2Token>;

getAccessTokenFromAuthorizationCodeFlow(
request: FastifyRequest,
callback: (err: any, token: OAuth2Token) => void,
): void;

getAccessTokenFromAuthorizationCodeFlow(
request: FastifyRequest,
reply: FastifyReply,
callback: (err: any, token: OAuth2Token) => void,
): void;

getNewAccessTokenUsingRefreshToken(
refreshToken: Token,
params: Object,
callback: (err: any, token: OAuth2Token) => void,
): void;

getNewAccessTokenUsingRefreshToken(refreshToken: Token, params: Object): Promise<OAuth2Token>;

generateAuthorizationUri(
request: FastifyRequest,
reply: FastifyReply,
callback: (err: any, uri: string) => void
): void

generateAuthorizationUri(
request: FastifyRequest,
reply: FastifyReply,
): Promise<string>;

revokeToken(
revokeToken: Token,
tokenType: TToken,
httpOptions: Object | undefined,
callback: (err: any) => void
): void;

revokeToken(revokeToken: Token, tokenType: TToken, httpOptions: Object | undefined): Promise<void>;

revokeAllToken(
revokeToken: Token,
httpOptions: Object | undefined,
callback: (err: any) => void
): void;

revokeAllToken(revokeToken: Token, httpOptions: Object | undefined): Promise<void>;

userinfo(tokenSetOrToken: Token | string): Promise<Object>;

userinfo(tokenSetOrToken: Token | string, userInfoExtraOptions: UserInfoExtraOptions | undefined): Promise<Object>;

userinfo(tokenSetOrToken: Token | string, callback: (err: any, userinfo: Object) => void): void;

userinfo(tokenSetOrToken: Token | string, userInfoExtraOptions: UserInfoExtraOptions | undefined, callback: (err: any, userinfo: Object) => void): void;
}
export type UserInfoExtraOptions = { method?: 'GET' | 'POST', via?: 'header' | 'body', params?: object };
export const fastifyOauth2: FastifyOauth2
export {fastifyOauth2 as default}
authorizationMethod?: 'header' | 'body' | undefined;
} | undefined;
}

export interface OAuth2Namespace {
getAccessTokenFromAuthorizationCodeFlow(
request: FastifyRequest,
): Promise<OAuth2Token>;

getAccessTokenFromAuthorizationCodeFlow(
request: FastifyRequest,
reply: FastifyReply,
): Promise<OAuth2Token>;

getAccessTokenFromAuthorizationCodeFlow(
request: FastifyRequest,
callback: (err: any, token: OAuth2Token) => void,
): void;

getAccessTokenFromAuthorizationCodeFlow(
request: FastifyRequest,
reply: FastifyReply,
callback: (err: any, token: OAuth2Token) => void,
): void;

getNewAccessTokenUsingRefreshToken(
refreshToken: Token,
params: Object,
callback: (err: any, token: OAuth2Token) => void,
): void;

getNewAccessTokenUsingRefreshToken(refreshToken: Token, params: Object): Promise<OAuth2Token>;

generateAuthorizationUri(
request: FastifyRequest,
reply: FastifyReply,
callback: (err: any, uri: string) => void
): void

generateAuthorizationUri(
request: FastifyRequest,
reply: FastifyReply,
): Promise<string>;

revokeToken(
revokeToken: Token,
tokenType: TToken,
httpOptions: Object | undefined,
callback: (err: any) => void
): void;

revokeToken(revokeToken: Token, tokenType: TToken, httpOptions: Object | undefined): Promise<void>;

revokeAllToken(
revokeToken: Token,
httpOptions: Object | undefined,
callback: (err: any) => void
): void;

revokeAllToken(revokeToken: Token, httpOptions: Object | undefined): Promise<void>;

userinfo(tokenSetOrToken: Token | string): Promise<Object>;

userinfo(tokenSetOrToken: Token | string, userInfoExtraOptions: UserInfoExtraOptions | undefined): Promise<Object>;

userinfo(tokenSetOrToken: Token | string, callback: (err: any, userinfo: Object) => void): void;

userinfo(tokenSetOrToken: Token | string, userInfoExtraOptions: UserInfoExtraOptions | undefined, callback: (err: any, userinfo: Object) => void): void;
}
export type UserInfoExtraOptions = { method?: 'GET' | 'POST', via?: 'header' | 'body', params?: object }
export const fastifyOauth2: FastifyOauth2
export { fastifyOauth2 as default }
}

declare function fastifyOauth2(...params: Parameters<FastifyOauth2>): ReturnType<FastifyOauth2>
declare function fastifyOauth2 (...params: Parameters<FastifyOauth2>): ReturnType<FastifyOauth2>

export = fastifyOauth2

type UpperCaseCharacters = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
type UpperCaseCharacters = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z'

declare module 'fastify' {
interface FastifyInstance {
// UpperCaseCharacters ensures that the name has at least one character in it + is a simple camel-case:ification
[key: `oauth2${UpperCaseCharacters}${string}`]: fastifyOauth2.OAuth2Namespace | undefined;
}
interface FastifyInstance {
// UpperCaseCharacters ensures that the name has at least one character in it + is a simple camel-case:ification
[key: `oauth2${UpperCaseCharacters}${string}`]: fastifyOauth2.OAuth2Namespace | undefined;
}
}
Loading
Loading