Skip to content
Draft
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
6,035 changes: 6,035 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

114 changes: 46 additions & 68 deletions src/custom/CortiAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,24 @@ import * as serializers from "../serialization/index.js";
import * as errors from "../errors/index.js";
import * as environments from "../environments.js";
import { getEnvironment } from "./utils/getEnvironmentFromString.js";
import { AuthGetTokenRequestCustom } from "./serialization/index.js";
import { AuthGetTokenRequestWithAuthorizationCode } from "./serialization/corti-auth/AuthGetTokenRequestWithAuthorizationCode.js";
import { AuthGetTokenRequestWithRefreshToken } from "./serialization/corti-auth/AuthGetTokenWithRefreshToken.js";

interface AuthorizationCodeClient {
clientId: string;
redirectUri: string;
}
export { AuthGetTokenRequestCustom };

interface AuthorizationCodeServer {
interface AuthorizationCodeClient {
clientId: string;
clientSecret: string;
redirectUri: string;
code: string;
}

interface AuthorizationRefreshServer {
clientId: string;
clientSecret: string;
refreshToken: string;
}

interface Options {
skipRedirect?: boolean;
}

type AuthOptions = Omit<FernAuth.Options, 'environment'> & {
type AuthOptions = Omit<FernAuth.Options, "environment"> & {
environment: core.Supplier<environments.CortiEnvironment | environments.CortiEnvironmentUrls> | string;
}
};

export class Auth extends FernAuth {
/**
Expand All @@ -65,32 +57,33 @@ export class Auth extends FernAuth {
request: Corti.AuthGetTokenRequest,
requestOptions?: FernAuth.RequestOptions,
): core.HttpResponsePromise<Corti.GetTokenResponse> {
return core.HttpResponsePromise.fromPromise(this.__getToken_custom(request, requestOptions));
return core.HttpResponsePromise.fromPromise(
this.__getToken_custom({ ...request, grantType: "client_credentials" }, requestOptions),
);
}

/**
* Patch: added method to get Authorization URL for Authorization code flow
*/
public async authorizeURL({
clientId,
redirectUri,
}: AuthorizationCodeClient, options?: Options): Promise<string> {
const authUrl = new URL(core.url.join(
(await core.Supplier.get(this._options.baseUrl)) ??
(await core.Supplier.get(this._options.environment)).login,
await core.Supplier.get(this._options.tenantName),
"protocol/openid-connect/auth",
));

authUrl.searchParams.set('response_type', 'code');
authUrl.searchParams.set('scope', 'openid profile');
public async authorizeURL({ clientId, redirectUri }: AuthorizationCodeClient, options?: Options): Promise<string> {
const authUrl = new URL(
core.url.join(
(await core.Supplier.get(this._options.baseUrl)) ??
(await core.Supplier.get(this._options.environment)).login,
await core.Supplier.get(this._options.tenantName),
"protocol/openid-connect/auth",
),
);

authUrl.searchParams.set("response_type", "code");
authUrl.searchParams.set("scope", "openid profile");

if (clientId !== undefined) {
authUrl.searchParams.set('client_id', clientId);
authUrl.searchParams.set("client_id", clientId);
}

if (redirectUri !== undefined) {
authUrl.searchParams.set('redirect_uri', redirectUri);
authUrl.searchParams.set("redirect_uri", redirectUri);
}

const authUrlString = authUrl.toString();
Expand All @@ -107,13 +100,18 @@ export class Auth extends FernAuth {
* Patch: calls __getToken_custom with additional fields to support Authorization code flow
*/
public getCodeFlowToken(
request: AuthorizationCodeServer,
request: Omit<AuthGetTokenRequestWithAuthorizationCode, "grantType">,
requestOptions?: FernAuth.RequestOptions,
): core.HttpResponsePromise<Corti.GetTokenResponse> {
return core.HttpResponsePromise.fromPromise(this.__getToken_custom({
...request,
grantType: "authorization_code",
}, requestOptions));
return core.HttpResponsePromise.fromPromise(
this.__getToken_custom(
{
...request,
grantType: "authorization_code",
},
requestOptions,
),
);
}

/**
Expand All @@ -123,12 +121,7 @@ export class Auth extends FernAuth {
/**
* Patch: added additional fields to request to support Authorization code flow
*/
request: Corti.AuthGetTokenRequest & Partial<{
grantType: "client_credentials" | "authorization_code" | "refresh_token";
code: string;
redirectUri: string;
refreshToken: string;
}>,
request: AuthGetTokenRequestCustom,
requestOptions?: FernAuth.RequestOptions,
): Promise<core.WithRawResponse<Corti.GetTokenResponse>> {
const _response = await core.fetcher({
Expand Down Expand Up @@ -158,31 +151,11 @@ export class Auth extends FernAuth {
* Patch: removed `requestType: "json"`, made body a URLSearchParams object
*/
body: new URLSearchParams({
...serializers.AuthGetTokenRequest.jsonOrThrow(request, {
...AuthGetTokenRequestCustom.jsonOrThrow(request, {
unrecognizedObjectKeys: "strip",
omitUndefined: true,
}),
scope: "openid",
/**
* Patch: `grant_type` uses values from request or defaults to "client_credentials"
*/
grant_type: request.grantType || "client_credentials",
/**
* Patch: added `code` and `redirect_uri` fields for Authorization code flow
* Patch: added `refresh_token` field for Refresh token flow
*/
...(request.grantType === "authorization_code"
? {
code: request.code,
redirect_uri: request.redirectUri
}
: {}),
...(request.grantType === "refresh_token"
? {
refresh_token: request.refreshToken,
}
: {}
),
}),
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions?.maxRetries,
Expand Down Expand Up @@ -232,12 +205,17 @@ export class Auth extends FernAuth {
* Patch: calls __getToken_custom with additional fields to support Refresh token flow
*/
public refreshToken(
request: AuthorizationRefreshServer,
request: Omit<AuthGetTokenRequestWithRefreshToken, "grantType">,
requestOptions?: FernAuth.RequestOptions,
): core.HttpResponsePromise<Corti.GetTokenResponse> {
return core.HttpResponsePromise.fromPromise(this.__getToken_custom({
...request,
grantType: "refresh_token",
}, requestOptions));
return core.HttpResponsePromise.fromPromise(
this.__getToken_custom(
{
...request,
grantType: "refresh_token",
},
requestOptions,
),
);
}
}
27 changes: 14 additions & 13 deletions src/custom/CortiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,20 @@ export class CortiClient {
/**
* Patch: if `clientId` is provided, use OAuthTokenProvider, otherwise use BearerProvider
*/
this._oauthTokenProvider = "clientId" in _options.auth ?
new core.OAuthTokenProvider({
clientId: _options.auth.clientId,
clientSecret: _options.auth.clientSecret,
/**
* Patch: provide whole `options` object to the Auth client, since it depends on both tenantName and environment
*/
authClient: new Auth(this._options),
}) :
new RefreshBearerProvider({
..._options.auth,
initialTokenResponse
});
this._oauthTokenProvider =
"clientId" in _options.auth
? new core.OAuthTokenProvider({
clientId: _options.auth.clientId,
clientSecret: _options.auth.clientSecret,
/**
* Patch: provide whole `options` object to the Auth client, since it depends on both tenantName and environment
*/
authClient: new Auth(this._options),
})
: new RefreshBearerProvider({
..._options.auth,
initialTokenResponse,
});
}

public get interactions(): Interactions {
Expand Down
26 changes: 26 additions & 0 deletions src/custom/serialization/corti-auth/AuthGetTokenRequestCustom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as core from "../../../core/index.js";

import { AuthGetTokenRequestWithAuthorizationCode } from "./AuthGetTokenRequestWithAuthorizationCode.js";
import { AuthGetTokenRequestWithRefreshToken } from "./AuthGetTokenWithRefreshToken.js";
import { AuthGetTokenRequestWithClientCredentials } from "./AuthGetTokenWithClientCredentials.js";

export const AuthGetTokenRequestCustom: core.serialization.Schema<
AuthGetTokenRequestCustom.Raw,
AuthGetTokenRequestCustom
> = core.serialization.undiscriminatedUnion([
AuthGetTokenRequestWithClientCredentials,
AuthGetTokenRequestWithAuthorizationCode,
AuthGetTokenRequestWithRefreshToken,
]);

export declare namespace AuthGetTokenRequestCustom {
export type Raw =
| AuthGetTokenRequestWithAuthorizationCode.Raw
| AuthGetTokenRequestWithRefreshToken.Raw
| AuthGetTokenRequestWithClientCredentials.Raw;
}

export type AuthGetTokenRequestCustom =
| AuthGetTokenRequestWithClientCredentials
| AuthGetTokenRequestWithAuthorizationCode
| AuthGetTokenRequestWithRefreshToken;
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as serializers from "../../../serialization/index.js";
import * as core from "../../../core/index.js";

export const AuthGetTokenRequestWithAuthorizationCode: core.serialization.Schema<
AuthGetTokenRequestWithAuthorizationCode.Raw,
AuthGetTokenRequestWithAuthorizationCode
> = core.serialization.object({
clientId: core.serialization.property(
"client_id",
core.serialization.string(),
),
clientSecret: core.serialization.property(
"client_secret",
core.serialization.string(),
),
grantType: core.serialization.property(
"grant_type",
core.serialization.stringLiteral("authorization_code"),
),
code: core.serialization.property("code", core.serialization.string()),
redirectUri: core.serialization.property(
"redirect_uri",
core.serialization.string(),
),
});

export declare namespace AuthGetTokenRequestWithAuthorizationCode {
export interface Raw extends serializers.AuthGetTokenRequest.Raw {
grant_type: "authorization_code";
code: string;
redirect_uri: string;
}
}

export type AuthGetTokenRequestWithAuthorizationCode = {
clientId: string;
clientSecret: string;
grantType: "authorization_code";
code: string;
redirectUri: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as serializers from "../../../serialization/index.js";
import * as core from "../../../core/index.js";

export const AuthGetTokenRequestWithClientCredentials: core.serialization.Schema<
AuthGetTokenRequestWithClientCredentials.Raw,
AuthGetTokenRequestWithClientCredentials
> = core.serialization.object({
clientId: core.serialization.property(
"client_id",
core.serialization.string(),
),
clientSecret: core.serialization.property(
"client_secret",
core.serialization.string(),
),
grantType: core.serialization.property(
"grant_type",
core.serialization.stringLiteral("client_credentials"),
),
});

export declare namespace AuthGetTokenRequestWithClientCredentials {
export interface Raw extends serializers.AuthGetTokenRequest.Raw {
grant_type: "client_credentials";
}
}

export type AuthGetTokenRequestWithClientCredentials = {
clientId: string;
clientSecret: string;
grantType: "client_credentials";
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as serializers from "../../../serialization/index.js";
import * as Corti from "../../../api/index.js";
import * as core from "../../../core/index.js";

export const AuthGetTokenRequestWithRefreshToken: core.serialization.Schema<
AuthGetTokenRequestWithRefreshToken.Raw,
AuthGetTokenRequestWithRefreshToken
> = core.serialization.object({
clientId: core.serialization.property(
"client_id",
core.serialization.string(),
),
clientSecret: core.serialization.property(
"client_secret",
core.serialization.string(),
),
grantType: core.serialization.property(
"grant_type",
core.serialization.stringLiteral("refresh_token"),
),
refreshToken: core.serialization.property(
"refresh_token",
core.serialization.string(),
),
});

export declare namespace AuthGetTokenRequestWithRefreshToken {
export interface Raw extends serializers.AuthGetTokenRequest.Raw {
grant_type: "refresh_token";
refresh_token: string;
}
}

export type AuthGetTokenRequestWithRefreshToken = {
clientId: string;
clientSecret: string;
grantType: "refresh_token";
refreshToken: string;
};
1 change: 1 addition & 0 deletions src/custom/serialization/corti-auth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { AuthGetTokenRequestCustom } from "./AuthGetTokenRequestCustom.js";
18 changes: 18 additions & 0 deletions src/custom/serialization/corti-client/CortiClientCustom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as serializers from "../../../serialization/index.js";
import * as Corti from "../../../api/index.js";
import * as core from "../../../core/index.js";

export const CortiClientCustom: core.serialization.Schema<CortiClientCustom.Raw, Corti.AuthGetTokenRequest> =
core.serialization.object({
clientId: core.serialization.property("client_id", core.serialization.string()),
clientSecret: core.serialization.property("client_secret", core.serialization.string()),
});

export declare namespace CortiClientCustom {
export interface Raw extends serializers.AuthGetTokenRequest.Raw {
grantType?: "client_credentials" | "authorization_code" | "refresh_token";
code?: string;
redirectUri?: string;
refreshToken?: string;
}
}
1 change: 1 addition & 0 deletions src/custom/serialization/corti-client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { CortiClientCustom } from "./CortiClientCustom.js";
2 changes: 2 additions & 0 deletions src/custom/serialization/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./corti-auth/index.js";
export * from "./corti-client/index.js";