tink/ts/tink.classes.tinkaccount.ts

179 lines
4.8 KiB
TypeScript
Raw Permalink Normal View History

2022-02-15 15:51:16 +00:00
import * as plugins from './tink.plugins';
2022-02-18 11:55:56 +00:00
2022-02-19 00:34:07 +00:00
import { TinkUser } from './tink.classes.tinkuser';
2022-02-18 11:55:56 +00:00
2022-02-15 15:51:16 +00:00
export class TinkAccount {
2022-02-19 00:34:07 +00:00
public clientId: string;
2022-02-19 12:53:12 +00:00
private _clientSecret: string;
2022-02-15 15:51:16 +00:00
2022-02-19 12:53:12 +00:00
private _apiBaseUrl: string = 'https://api.tink.com';
2022-02-18 11:55:56 +00:00
2022-02-15 15:51:16 +00:00
constructor(clientIdArg: string, clientSecretArg: string) {
this.clientId = clientIdArg;
2022-02-19 12:53:12 +00:00
this._clientSecret = clientSecretArg;
2022-02-15 15:51:16 +00:00
}
2022-02-15 22:50:37 +00:00
public async getTinkHealthyBoolean(): Promise<boolean> {
const response = await plugins.smartrequest.request(
'https://api.tink.com/api/v1/monitoring/healthy',
2022-02-19 12:15:59 +00:00
{
keepAlive: false,
}
2022-02-15 22:50:37 +00:00
);
2022-02-15 15:51:16 +00:00
return response.body === 'ok';
}
2022-02-19 00:34:07 +00:00
public async getClientAccessTokenForScope(scopeArg: string): Promise<string> {
2022-02-15 15:51:16 +00:00
// lets get an accessToken for the request
2022-02-15 22:50:37 +00:00
const response = await plugins.smartrequest.postFormDataUrlEncoded(
2022-02-19 12:53:12 +00:00
`${this._apiBaseUrl}/api/v1/oauth/token`,
2022-02-19 12:15:59 +00:00
{
keepAlive: false,
},
2022-02-15 22:50:37 +00:00
[
{
key: 'client_id',
content: this.clientId,
},
{
key: 'client_secret',
2022-02-19 12:53:12 +00:00
content: this._clientSecret,
2022-02-15 22:50:37 +00:00
},
{
key: 'grant_type',
content: 'client_credentials',
},
{
key: 'scope',
2022-02-19 00:34:07 +00:00
content: scopeArg,
2022-02-15 22:50:37 +00:00
},
]
);
2022-02-18 11:55:56 +00:00
if (response.statusCode !== 200) {
2022-02-19 12:15:59 +00:00
console.log(response.statusCode);
console.log(response.body);
2022-02-18 11:55:56 +00:00
throw new Error('there was an error aquiring an access token.');
}
2022-02-19 00:34:07 +00:00
const clientAccessToken = response.body.access_token;
return clientAccessToken;
}
2022-02-19 00:37:47 +00:00
public async getUserAuthorizationCode(
2022-02-19 00:34:07 +00:00
externalUserIdArg: string,
actorCLientIdArg: string,
scopeArg: string
) {
const accessToken = await this.getClientAccessTokenForScope('authorization:grant');
const response = await plugins.smartrequest.postFormDataUrlEncoded(
2022-02-19 12:53:12 +00:00
`${this._apiBaseUrl}/api/v1/oauth/authorization-grant/delegate`,
2022-02-19 00:34:07 +00:00
{
2022-02-19 12:15:59 +00:00
keepAlive: false,
2022-02-19 00:34:07 +00:00
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(
2022-02-19 12:53:12 +00:00
`${this._apiBaseUrl}/api/v1/oauth/token`,
2022-02-19 12:15:59 +00:00
{
keepAlive: false,
},
2022-02-19 00:34:07 +00:00
[
{
key: 'code',
content: authorizationCode,
},
{
key: 'client_id',
content: this.clientId,
},
{
key: 'client_secret',
2022-02-19 12:53:12 +00:00
content: this._clientSecret,
2022-02-19 00:34:07 +00:00
},
{
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...');
}
2022-02-19 12:53:12 +00:00
const response = await plugins.smartrequest.request(`${this._apiBaseUrl}${optionsArg.urlArg}`, {
2022-02-19 12:15:59 +00:00
keepAlive: false,
2022-02-18 11:55:56 +00:00
headers: {
2022-02-19 00:34:07 +00:00
Authorization: `Bearer ${optionsArg.accessToken}`,
'Content-Type': 'application/json',
2022-02-18 11:55:56 +00:00
},
method: optionsArg.methodArg,
2022-02-19 00:34:07 +00:00
requestBody: JSON.stringify(optionsArg.payloadArg),
});
console.log(response.statusCode);
return response.body;
2022-02-18 11:55:56 +00:00
}
2022-02-19 12:15:59 +00:00
public async getTinkUser(externalUserIdArg: string) {
const tinkuser = await TinkUser.getTinkUser(this, externalUserIdArg);
return tinkuser;
}
2022-02-18 11:55:56 +00:00
public async createTinkUser(externalUserIdArg: string) {
const tinkuser = await TinkUser.createNewTinkUser(this, externalUserIdArg);
2022-02-19 00:34:07 +00:00
return tinkuser;
2022-02-15 22:50:37 +00:00
}
}