tink/ts/tink.classes.tinkaccount.ts
2022-02-19 11:43:10 +01:00

164 lines
4.4 KiB
TypeScript

import * as plugins from './tink.plugins';
import { TinkUser } from './tink.classes.tinkuser';
export class TinkAccount {
public clientId: string;
private clientSecret: string;
private apiBaseUrl: string = 'https://api.tink.com';
constructor(clientIdArg: string, clientSecretArg: string) {
this.clientId = clientIdArg;
this.clientSecret = clientSecretArg;
}
public async getTinkHealthyBoolean(): Promise<boolean> {
const response = await plugins.smartrequest.request(
'https://api.tink.com/api/v1/monitoring/healthy',
{}
);
return response.body === 'ok';
}
public async getClientAccessTokenForScope(scopeArg: string): Promise<string> {
// lets get an accessToken for the request
const response = await plugins.smartrequest.postFormDataUrlEncoded(
`${this.apiBaseUrl}/api/v1/oauth/token`,
{},
[
{
key: 'client_id',
content: this.clientId,
},
{
key: 'client_secret',
content: this.clientSecret,
},
{
key: 'grant_type',
content: 'client_credentials',
},
{
key: 'scope',
content: scopeArg,
},
]
);
if (response.statusCode !== 200) {
throw new Error('there was an error aquiring an access token.');
}
const clientAccessToken = response.body.access_token;
return clientAccessToken;
}
public async getUserAuthorizationCode(
externalUserIdArg: string,
actorCLientIdArg: string,
scopeArg: string
) {
const accessToken = await this.getClientAccessTokenForScope('authorization:grant');
const response = await plugins.smartrequest.postFormDataUrlEncoded(
`${this.apiBaseUrl}/api/v1/oauth/authorization-grant/delegate`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
[
{
key: 'response_type',
content: 'code',
},
{
key: 'actor_client_id',
content: actorCLientIdArg,
},
{
key: 'external_user_id',
content: externalUserIdArg,
},
{
key: 'id_hint',
content: 'Hello there',
},
{
key: 'scope',
content: scopeArg,
},
]
);
if (response.statusCode !== 200) {
console.log(response.body);
throw new Error('there was an error aquiring an access token.');
}
const userAuthorizationCode = response.body.code;
return userAuthorizationCode;
}
public async getUserAccessToken(authorizationCode: string): Promise<string> {
const accessToken = await this.getClientAccessTokenForScope('authorization:grant');
const response = await plugins.smartrequest.postFormDataUrlEncoded(
`${this.apiBaseUrl}/api/v1/oauth/token`,
{},
[
{
key: 'code',
content: authorizationCode,
},
{
key: 'client_id',
content: this.clientId,
},
{
key: 'client_secret',
content: this.clientSecret,
},
{
key: 'grant_type',
content: 'authorization_code',
},
]
);
if (response.statusCode !== 200) {
console.log(response.body);
throw new Error('there was an error aquiring an access token.');
}
const userAccessToken = response.body.access_token;
return userAccessToken;
}
// the request method for tink respecting platform specific stuff
// e.g. certain headers if needed
public async request(optionsArg: {
urlArg: string;
methodArg: 'POST' | 'GET';
accessToken: string;
payloadArg: any;
}) {
// check health
if (!(await this.getTinkHealthyBoolean())) {
throw new Error('TINK is not healthy right now. Please try again later.');
} else {
console.log('tink is healthy, continuing...');
}
const response = await plugins.smartrequest.request(`${this.apiBaseUrl}${optionsArg.urlArg}`, {
headers: {
Authorization: `Bearer ${optionsArg.accessToken}`,
'Content-Type': 'application/json',
},
method: optionsArg.methodArg,
requestBody: JSON.stringify(optionsArg.payloadArg),
});
console.log(response.statusCode);
return response.body;
}
public async createTinkUser(externalUserIdArg: string) {
const tinkuser = await TinkUser.createNewTinkUser(this, externalUserIdArg);
return tinkuser;
}
}