From 337f41e7235642fac06a8a73ac86c90001dba4ef Mon Sep 17 00:00:00 2001 From: Guilherme Werner Date: Sun, 24 Dec 2023 08:19:55 -0300 Subject: [PATCH] Add oauth2 types --- src/index.ts | 29 +++++++++++++++++- src/oauth2.ts | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 3 ++ 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/oauth2.ts diff --git a/src/index.ts b/src/index.ts index c0e82a6..4208e6c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,4 +30,31 @@ export class TribufuClient { public getId(): BigInt { return this.id; } -} +}; + +import { + OAuth2AuthorizeRequest, + OAuth2CodeResponse, + OAuth2RevokeRequest, + OAuth2TokenRequest, + OAuth2TokenResponse, +} from './oauth2'; + +import type { + OAuth2GrantType, + OAuth2ResponseType, + OAuth2TokenHintType, + OAuth2TokenType, +} from './oauth2'; + +export { + OAuth2AuthorizeRequest, + OAuth2CodeResponse, + OAuth2GrantType, + OAuth2ResponseType, + OAuth2RevokeRequest, + OAuth2TokenHintType, + OAuth2TokenRequest, + OAuth2TokenResponse, + OAuth2TokenType, +}; diff --git a/src/oauth2.ts b/src/oauth2.ts new file mode 100644 index 0000000..4735a90 --- /dev/null +++ b/src/oauth2.ts @@ -0,0 +1,83 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +export type OAuth2ResponseType = "code" | "token"; + +export type OAuth2TokenType = "bearer"; + +export type OAuth2GrantType = "authorization_code" | "refresh_token" | "password"; + +export type OAuth2TokenHintType = "refresh_token" | "access_token"; + +export class OAuth2AuthorizeRequest { + response_type: OAuth2ResponseType; + client_id: string; + scope: string | null; + redirect_uri: string; + state: string | null; + + constructor(client_id: string, redirect_uri: string, response_type: OAuth2ResponseType, scope: string, state: string) { + this.client_id = client_id; + this.redirect_uri = redirect_uri; + this.response_type = response_type; + this.scope = scope; + this.state = state; + } +}; + +export class OAuth2CodeResponse { + code: string; + state: string | null; + + constructor(code: string, state: string) { + this.code = code; + this.state = state; + } +}; + +export class OAuth2TokenRequest { + grant_type: OAuth2GrantType; + code: string | null; + refresh_token: string | null; + username: string | null; + password: string | null; + client_id: string; + client_secret: string; + redirect_uri: string | null; + + constructor(grant_type: OAuth2GrantType, code: string, refresh_token: string, username: string, password: string, client_id: string, client_secret: string, redirect_uri: string) { + this.grant_type = grant_type; + this.code = code; + this.refresh_token = refresh_token; + this.username = username; + this.password = password; + this.client_id = client_id; + this.client_secret = client_secret; + this.redirect_uri = redirect_uri; + } +}; + +export class OAuth2TokenResponse { + token_type: OAuth2TokenType; + access_token: string; + refresh_token: string; + scope: string | null; + expires_in: number; + + constructor(token_type: OAuth2TokenType, access_token: string, refresh_token: string, scope: string, expires_in: number) { + this.token_type = token_type; + this.access_token = access_token; + this.refresh_token = refresh_token; + this.scope = scope; + this.expires_in = expires_in; + } +}; + +export class OAuth2RevokeRequest { + token: string; + token_type_hint: OAuth2TokenHintType; + + constructor(token: string, token_type_hint: OAuth2TokenHintType) { + this.token = token; + this.token_type_hint = token_type_hint; + } +}; diff --git a/tsconfig.json b/tsconfig.json index 444a833..1b1d8a9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,6 +8,9 @@ "emitDeclarationOnly": true, "forceConsistentCasingInFileNames": true, }, + "include": [ + "src/**/*", + ], "exclude": [ "build", "node_modules",