Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
68a3fcb06f | |||
2433b0d7b2 | |||
570a1cd6b2 | |||
9425a85150 | |||
2b902aa31b | |||
7bb93a5edf | |||
4c10678b17 | |||
eb61d1abbf |
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/tink",
|
"name": "@mojoio/tink",
|
||||||
"version": "1.0.5",
|
"version": "1.0.9",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@mojoio/tink",
|
"name": "@mojoio/tink",
|
||||||
"version": "1.0.5",
|
"version": "1.0.9",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartrequest": "^1.1.56"
|
"@pushrocks/smartrequest": "^1.1.56"
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/tink",
|
"name": "@mojoio/tink",
|
||||||
"version": "1.0.5",
|
"version": "1.0.9",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "an unofficial api abstraction for tink.com",
|
"description": "an unofficial api abstraction for tink.com",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
|
@ -19,8 +19,17 @@ tap.test('should report tink as healthy', async () => {
|
|||||||
await expectAsync(tinkTestAccount.getTinkHealthyBoolean()).toBeTrue();
|
await expectAsync(tinkTestAccount.getTinkHealthyBoolean()).toBeTrue();
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should create a valid request', async () => {
|
tap.test('should create a valid request', async (toolsArg) => {
|
||||||
await tinkTestAccount.request('', 'POST', '', {});
|
const tinkuser = await tinkTestAccount.createTinkUser("user_1234_abc");
|
||||||
|
console.log(await tinkuser.getTinkLink());
|
||||||
|
process.exit(0);
|
||||||
|
await toolsArg.delayFor(10000);
|
||||||
|
})
|
||||||
|
|
||||||
|
tap.test('should delete existing users', async () => {
|
||||||
|
expect(tinkTestAccount).toBeInstanceOf(tink.TinkAccount);
|
||||||
|
const tinkUser = new tink.TinkUser(tinkTestAccount, null, 'user_1234_abc');
|
||||||
|
await tinkUser.delete();
|
||||||
})
|
})
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -1 +1,2 @@
|
|||||||
export * from './tink.classes.tinkaccount';
|
export * from './tink.classes.tinkaccount';
|
||||||
|
export * from './tink.classes.tinkuser';
|
@ -1,8 +1,13 @@
|
|||||||
import * as plugins from './tink.plugins';
|
import * as plugins from './tink.plugins';
|
||||||
|
|
||||||
|
import { TinkUser } from './tink.classes.tinkuser';
|
||||||
|
|
||||||
export class TinkAccount {
|
export class TinkAccount {
|
||||||
private clientId: string;
|
public clientId: string;
|
||||||
private clientSecret: string;
|
private clientSecret: string;
|
||||||
|
|
||||||
|
private apiBaseUrl: string = 'https://api.tink.com';
|
||||||
|
|
||||||
constructor(clientIdArg: string, clientSecretArg: string) {
|
constructor(clientIdArg: string, clientSecretArg: string) {
|
||||||
this.clientId = clientIdArg;
|
this.clientId = clientIdArg;
|
||||||
this.clientSecret = clientSecretArg;
|
this.clientSecret = clientSecretArg;
|
||||||
@ -16,23 +21,10 @@ export class TinkAccount {
|
|||||||
return response.body === 'ok';
|
return response.body === 'ok';
|
||||||
}
|
}
|
||||||
|
|
||||||
// the request method for tink respecting platform specific stuff
|
public async getClientAccessTokenForScope(scopeArg: string): Promise<string> {
|
||||||
// e.g. certain headers if needed
|
|
||||||
public async request(
|
|
||||||
urlArg: string,
|
|
||||||
methodArg: 'POST' | 'GET',
|
|
||||||
scopeArg: 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...');
|
|
||||||
}
|
|
||||||
// lets get an accessToken for the request
|
// lets get an accessToken for the request
|
||||||
const response = await plugins.smartrequest.postFormDataUrlEncoded(
|
const response = await plugins.smartrequest.postFormDataUrlEncoded(
|
||||||
'https://api.tink.com/api/v1/oauth/token',
|
`${this.apiBaseUrl}/api/v1/oauth/token`,
|
||||||
{},
|
{},
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
@ -49,11 +41,123 @@ export class TinkAccount {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'scope',
|
key: 'scope',
|
||||||
content: 'user:read',
|
content: scopeArg,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
console.log(response.statusCode);
|
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);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1 +1,65 @@
|
|||||||
import * as plugins from './tink.plugins';
|
import * as plugins from './tink.plugins';
|
||||||
|
|
||||||
|
import { TinkAccount } from './tink.classes.tinkaccount';
|
||||||
|
|
||||||
|
export class TinkUser {
|
||||||
|
// STATIC
|
||||||
|
public static async createNewTinkUser(tinkaccountArg: TinkAccount, externalUserIdArg: string) {
|
||||||
|
const accessToken = await tinkaccountArg.getClientAccessTokenForScope('user:create');
|
||||||
|
const response = await tinkaccountArg.request({
|
||||||
|
urlArg: '/api/v1/user/create',
|
||||||
|
accessToken,
|
||||||
|
methodArg: 'POST',
|
||||||
|
payloadArg: {
|
||||||
|
external_user_id: externalUserIdArg,
|
||||||
|
market: 'DE',
|
||||||
|
locale: 'en_US',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const newTinkUser = new TinkUser(tinkaccountArg, response.user_id, response.external_user_id);
|
||||||
|
return newTinkUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
public tinkAccountRef: TinkAccount;
|
||||||
|
public tinkUserId: string;
|
||||||
|
public externalUserIdArg: string;
|
||||||
|
|
||||||
|
public authorizationToken: string;
|
||||||
|
|
||||||
|
constructor(tinkAccountrefArg: TinkAccount, tinkUserIdArg: string, externalUserIdArg: string) {
|
||||||
|
this.tinkAccountRef = tinkAccountrefArg;
|
||||||
|
this.tinkUserId = tinkUserIdArg;
|
||||||
|
this.externalUserIdArg = externalUserIdArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* deletes the user
|
||||||
|
*/
|
||||||
|
public async delete() {
|
||||||
|
const authorizationCode = await this.tinkAccountRef.getUserAuthorizationCode(
|
||||||
|
this.externalUserIdArg,
|
||||||
|
this.tinkAccountRef.clientId,
|
||||||
|
'user:delete'
|
||||||
|
);
|
||||||
|
const accessToken = await this.tinkAccountRef.getUserAccessToken(authorizationCode);
|
||||||
|
const response = await this.tinkAccountRef.request({
|
||||||
|
methodArg: 'POST',
|
||||||
|
accessToken,
|
||||||
|
payloadArg: {},
|
||||||
|
urlArg: '/api/v1/user/delete',
|
||||||
|
});
|
||||||
|
console.log(`successfully deleted user with externalId ${this.externalUserIdArg}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getTinkLink(): Promise<string> {
|
||||||
|
const authorizationCode = await this.tinkAccountRef.getUserAuthorizationCode(
|
||||||
|
this.externalUserIdArg,
|
||||||
|
'df05e4b379934cd09963197cc855bfe9', // this is a hardcoded app id for tink link, as recommended by tink.com
|
||||||
|
'authorization:read,authorization:grant,credentials:refresh,credentials:read,credentials:write,providers:read,user:read'
|
||||||
|
);
|
||||||
|
const tinkLinkUrl = `https://link.tink.com/1.0/business-transactions/connect-accounts?client_id=${'teststate'}&redirect_uri=https://console.tink.com/callback&authorization_code=${authorizationCode}&market=DE`;
|
||||||
|
return tinkLinkUrl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user