Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also support object parameter and fix optionals #37

Closed
wants to merge 1 commit into from
Closed
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
67 changes: 56 additions & 11 deletions src/FusionAuthClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,40 @@ import {URLSearchParams} from "url";
export class FusionAuthClient {
public clientBuilder: IRESTClientBuilder = new DefaultRESTClientBuilder();
public credentials: RequestCredentials;
public host: string;
public apiKey?: string | null;
public tenantId?: string;

constructor(config: FusionAuthClientConfig);
constructor(
public apiKey: string,
public host: string,
public tenantId?: string,
) { }
apiKey: string,
host: string,
tenantId?: string,
);

constructor(
apiKeyOrConfig: string | FusionAuthClientConfig,
host?: string,
tenantId?: string,
) {
if (typeof apiKeyOrConfig === 'string') {
this.apiKey = apiKeyOrConfig;
this.host = host;
this.tenantId = tenantId;
} else {
this.apiKey = apiKeyOrConfig.apiKey;
this.host = apiKeyOrConfig.host;
this.tenantId = apiKeyOrConfig.tenantId;
}
}

/**
* Sets the tenant id, that will be included in the X-FusionAuth-TenantId header.
*
* @param {string | null} tenantId The value of the X-FusionAuth-TenantId header.
* @returns {FusionAuthClient}
*/
setTenantId(tenantId: string | null): FusionAuthClient {
setTenantId(tenantId?: string | null): FusionAuthClient {
this.tenantId = tenantId;
return this;
}
Expand Down Expand Up @@ -899,6 +919,10 @@ export class FusionAuthClient {
.go();
}

exchangeOAuthCodeForAccessToken(config: ExchangeOAuthCodeForAccessTokenConfig): Promise<ClientResponse<AccessToken>>;

exchangeOAuthCodeForAccessToken(code: string, client_id: string, client_secret: string, redirect_uri: string): Promise<ClientResponse<AccessToken>>;

/**
* Exchanges an OAuth authorization code for an access token.
* If you will be using the Authorization Code grant, you will make a request to the Token endpoint to exchange the authorization code returned from the Authorize endpoint for an access token.
Expand All @@ -909,14 +933,23 @@ export class FusionAuthClient {
* @param {string} redirect_uri The URI to redirect to upon a successful request.
* @returns {Promise<ClientResponse<AccessToken>>}
*/
exchangeOAuthCodeForAccessToken(code: string, client_id: string, client_secret: string, redirect_uri: string): Promise<ClientResponse<AccessToken>> {
exchangeOAuthCodeForAccessToken(codeOrConfig?: string | ExchangeOAuthCodeForAccessTokenConfig, client_id?: string, client_secret?: string, redirect_uri?: string): Promise<ClientResponse<AccessToken>> {
let body = new URLSearchParams();

body.append('code', code);
body.append('client_id', client_id);
body.append('client_secret', client_secret);
body.append('grant_type', 'authorization_code');
body.append('redirect_uri', redirect_uri);
if (typeof codeOrConfig === 'string') {
body.append('code', codeOrConfig);
body.append('client_id', client_id);
body.append('client_secret', client_secret);
body.append('grant_type', 'authorization_code');
body.append('redirect_uri', redirect_uri);
} else {
body.append('code', codeOrConfig.code);
body.append('client_id', codeOrConfig.client_id);
body.append('client_secret', codeOrConfig.client_secret);
body.append('grant_type', 'authorization_code');
body.append('redirect_uri', codeOrConfig.redirect_uri);
}

return this.startAnonymous<AccessToken, OAuthError>()
.withUri('/oauth2/token')
.withFormData(body)
Expand Down Expand Up @@ -3459,6 +3492,11 @@ export default FusionAuthClient;
*/
export type UUID = string;

export interface FusionAuthClientConfig {
host: string;
apiKey?: string;
tenantId?: string;
}

/**
* @author Daniel DeGroff
Expand Down Expand Up @@ -4307,6 +4345,13 @@ export enum EventType {
Test = "test"
}

export interface ExchangeOAuthCodeForAccessTokenConfig {
code: string;
client_id?: string;
client_secret?: string;
redirect_uri: string;
}

/**
* @author Brian Pontarelli
*/
Expand Down