Skip to content

Commit

Permalink
feat: add custom login flow method [release] (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Jul 21, 2021
1 parent 8d9aced commit d99a2aa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
47 changes: 44 additions & 3 deletions packages/core/src/baseCogniteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,17 @@ export type OIDC_AUTHORIZATION_CODE_FLOW = {
type: 'OIDC_AUTHORIZATION_CODE_FLOW';
options: OAuthLoginForOIDCAuthFlowOptions;
};
export type CUSTOM = {
type: 'CUSTOM';
options: unknown;
};

export type AuthFlowType =
| AAD_OAUTH
| CDF_OAUTH
| ADFS_OAUTH
| OIDC_AUTHORIZATION_CODE_FLOW;
| OIDC_AUTHORIZATION_CODE_FLOW
| CUSTOM;

/**
* @deprecated
Expand Down Expand Up @@ -365,6 +370,42 @@ export default class BaseCogniteClient {
return token !== null;
};

public async loginWithCustom(
fn: (
callbacks: {
setCluster: (s: string) => void;
setBearerToken: (s: string) => void;
validateAccessToken: (s: string) => Promise<boolean>;
}
) => Promise<OAuthLoginResult>
): Promise<boolean> {
if (this.hasBeenLoggedIn) {
throwReLogginError();
}

this.flow = { type: 'CUSTOM', options: {} };

const [authenticate, token] = await fn({
setCluster: this.httpClient.setCluster,
setBearerToken: this.httpClient.setBearerToken,
validateAccessToken: this.validateAccessToken,
});

this.httpClient.set401ResponseHandler(async (_, retry, reject) => {
const didAuthenticate = await authenticate();
return didAuthenticate ? retry() : reject();
});

if (token) {
this.httpClient.setBearerToken(token);
}

this.authenticate = authenticate;
this.hasBeenLoggedIn = true;

return token !== null;
}

/**
* To modify the base-url at any point in time
*/
Expand Down Expand Up @@ -898,7 +939,7 @@ export default class BaseCogniteClient {
}
}

protected async validateAccessToken(token: string): Promise<boolean> {
protected validateAccessToken = async (token: string): Promise<boolean> => {
try {
const response = await this.httpClient.get<any>(
`/api/${this.apiVersion}/token/inspect`,
Expand All @@ -917,7 +958,7 @@ export default class BaseCogniteClient {

throw err;
}
}
};
}

export type BaseRequestOptions = HttpRequestOptions;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/httpClient/basicHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export class BasicHttpClient {
return this.baseUrl;
}

public setCluster(cluster: string) {
public setCluster = (cluster: string) => {
this.baseUrl = `https://${cluster}.${DEFAULT_DOMAIN}`;
}
};

public get<ResponseType>(path: string, options: HttpRequestOptions = {}) {
return this.request<ResponseType>({
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/httpClient/cdfHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ export class CDFHttpClient extends RetryableHttpClient {
return this;
}

public setBearerToken(token: string) {
public setBearerToken = (token: string) => {
this.setDefaultHeader(AUTHORIZATION_HEADER, bearerString(token));
}
};

public set401ResponseHandler(handler: Response401Handler) {
this.response401Handler = handler;
Expand Down

0 comments on commit d99a2aa

Please sign in to comment.