From 3f8962ad6caa7a037b1bc830cbe0b69909463395 Mon Sep 17 00:00:00 2001 From: Akhil Date: Sun, 23 Jul 2023 08:51:03 +0000 Subject: [PATCH] feat: oauth2 login endpoints --- src/constants.ts | 7 ++++ src/types.ts | 62 +++++++++++++++++++++++++++++++- src/zbd.ts | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+), 1 deletion(-) diff --git a/src/constants.ts b/src/constants.ts index 653beb8..0c4450a 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -39,4 +39,11 @@ export const API = { DECODE_INVOICE_ENDPOINT: '/v0/decode-invoice', FETCH_ZBD_PROD_IPS_ENDPOINT: '/v0/prod-ips', BTCUSD_PRICE_TICKER_ENDPOINT: '/v0/btcusd', + + //OAuth2 + OAUTH2_AUTHORIZATION_ENDPOINT: '/v1/oauth2/authorize', + OAUTH2_GET_TOKEN_ENDPOINT: '/v1/oauth2/token', + OAUTH2_GET_USER_PROFILE_ENDPOINT: '/v1/oauth2/user', + OAUTH2_GET_USER_WALLET_ENDPOINT: '/v1/oauth2/wallet' + } diff --git a/src/types.ts b/src/types.ts index a9eda6e..6b3d7e8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -360,4 +360,64 @@ export interface FetchChargeFromGamertagOptionsType { internalId: string; } - \ No newline at end of file +export interface OAuth2AuthorizationRequestType { + clientId: string; + responseType: string; + redirectUri: string; + codeChallengeMethod: string; + codeChallenge: string; + scope: string; +} + +export interface OAuth2AuthorizationResponseType { + data: { + accessToken: string; + }, + success: boolean; +} + +export interface OAuth2GetAccessTokenRequestType { + clientId: string; + clientSecret: string; + grantType: string; + redirectUri: string; + code: string; + codeVerifier: string; +} + +export interface OAuth2GetRefreshTokenRequestType { + clientId: string; + clientSecret: string; + grantType: string; + redirectUri: string; + refreshToken: string; +} + +export interface GetUserDataResponseType { + data: { + email: string; + gamertag: string; + id: string; + image: string; + isVerified: string; + ligthningAddress: string; + publicBio: string; + publicStaticCharge: string; + social: Record; + }, + success: boolean; +} + +export interface GetUserWalletDataResponseType { + data: { + balance: string; + remainingAmountLimits: { + daily: string; + maxCredit: string; + monthly: string; + weekly: string; + }, + }, + message: string; + success: boolean; +} diff --git a/src/zbd.ts b/src/zbd.ts index 10e1927..c298a89 100644 --- a/src/zbd.ts +++ b/src/zbd.ts @@ -28,6 +28,9 @@ import { ValidateLightningAddressDataResponseType, SendLightningAddressPaymentDataResponseType, CreateChargeFromLightningAddressOptionsType, + OAuth2AuthorizationRequestType, + OAuth2GetAccessTokenRequestType, + OAuth2GetRefreshTokenRequestType, } from './types'; class zbd { @@ -397,6 +400,96 @@ class zbd { return response; } + + async authorizeOAuth2(options: OAuth2AuthorizationRequestType) { + + const queryParams = new URLSearchParams(); + queryParams.append('client_id', options.clientId); + queryParams.append('response_type', options.responseType); + queryParams.append('redirect_uri', options.redirectUri); + queryParams.append('code_challenge', options.codeChallenge); + queryParams.append('code_challenge_method', options.codeChallengeMethod); + + const response = await getData({ + url: `${API_URL}${API.OAUTH2_AUTHORIZATION_ENDPOINT}?${queryParams}`, + headers: { ...this.apiCoreHeaders }, + }); + + return response; + } + + async getOAuth2AccessToken(options: OAuth2GetAccessTokenRequestType) { + + const queryParams = new URLSearchParams({ + 'Content-Type': 'application/json' + }); + + const requestBody = { + client_id: options.clientId, + client_secret: options.clientSecret, + grant_type: options.grantType, + redirect_uri: options.redirectUri, + code: options.code, + code_verifier: options.codeVerifier, + } + + const response = await postData({ + url: `${API_URL}${API.OAUTH2_GET_TOKEN_ENDPOINT}?${queryParams}`, + headers: { + ...this.apiCoreHeaders, + }, + body: requestBody, + }); + + return response; + } + + async refreshOAuth2Token(options: OAuth2GetRefreshTokenRequestType) { + + const requestBody = { + client_id: options.clientId, + client_secret: options.clientSecret, + grant_type: options.grantType, + redirect_uri: options.redirectUri, + refresh_token: options.refreshToken, + } + + const response = await postData({ + url: `${API_URL}${API.OAUTH2_GET_TOKEN_ENDPOINT}`, + headers: { + ...this.apiCoreHeaders, + }, + body: requestBody, + }); + + return response; + } + + async getUser(oauth2AccessToken: string) { + + const response = await getData({ + url: `${API_URL}${API.OAUTH2_GET_USER_PROFILE_ENDPOINT}`, + headers: { + ...this.apiCoreHeaders, + usertoken: oauth2AccessToken, + }, + }); + + return response; + } + + async getUserWallet(oauth2AccessToken: string) { + + const response = await getData({ + url: `${API_URL}${API.OAUTH2_GET_USER_WALLET_ENDPOINT}`, + headers: { + ...this.apiCoreHeaders, + usertoken: oauth2AccessToken, + }, + }); + + return response; + } } export { zbd };