Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
18835fa5ae | |||
c0e26cdc4b | |||
68a3fcb06f | |||
2433b0d7b2 | |||
570a1cd6b2 | |||
9425a85150 | |||
2b902aa31b | |||
7bb93a5edf |
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@mojoio/tink",
|
||||
"version": "1.0.6",
|
||||
"version": "1.0.10",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@mojoio/tink",
|
||||
"version": "1.0.6",
|
||||
"version": "1.0.10",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@pushrocks/smartrequest": "^1.1.56"
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@mojoio/tink",
|
||||
"version": "1.0.6",
|
||||
"version": "1.0.10",
|
||||
"private": false,
|
||||
"description": "an unofficial api abstraction for tink.com",
|
||||
"main": "dist_ts/index.js",
|
||||
|
@ -27,6 +27,10 @@ Platform support | [ or [contribute monthly](https://lossless.link/contribute). :)
|
||||
|
@ -7,16 +7,6 @@ import * as tink from '../ts/index';
|
||||
|
||||
let tinkTestAccount: tink.TinkAccount;
|
||||
|
||||
tap.preTask('should delete existing users', async () => {
|
||||
const preTinkAccount = tinkTestAccount = new tink.TinkAccount(
|
||||
testQenv.getEnvVarOnDemand('TINK_CLIENT_ID'),
|
||||
testQenv.getEnvVarOnDemand('TINK_CLIENT_SECRET')
|
||||
);
|
||||
expect(tinkTestAccount).toBeInstanceOf(tink.TinkAccount);
|
||||
const tinkUser = new tink.TinkUser(preTinkAccount, null, 'user_1234_abc');
|
||||
await tinkUser.delete();
|
||||
})
|
||||
|
||||
tap.test('should create a valid tink account', async () => {
|
||||
tinkTestAccount = new tink.TinkAccount(
|
||||
testQenv.getEnvVarOnDemand('TINK_CLIENT_ID'),
|
||||
@ -29,8 +19,19 @@ tap.test('should report tink as healthy', async () => {
|
||||
await expectAsync(tinkTestAccount.getTinkHealthyBoolean()).toBeTrue();
|
||||
});
|
||||
|
||||
tap.test('should create a valid request', async () => {
|
||||
await tinkTestAccount.createTinkUser("user_1234_abc");
|
||||
tap.test('should create a valid request', async (toolsArg) => {
|
||||
const tinkuser: tink.TinkUser = await tinkTestAccount.getTinkUser("user_1234_abc");
|
||||
console.log(tinkuser);
|
||||
console.log(await tinkuser.getTinkLinkForMarket()); // defaults to 'DE';
|
||||
console.log(await tinkuser.getProviderConsents())
|
||||
await toolsArg.delayFor(10000);
|
||||
})
|
||||
|
||||
tap.test('should delete existing users', async () => {
|
||||
process.exit(0);
|
||||
expect(tinkTestAccount).toBeInstanceOf(tink.TinkAccount);
|
||||
const tinkUser = new tink.TinkUser(tinkTestAccount, null, 'user_1234_abc');
|
||||
await tinkUser.delete();
|
||||
})
|
||||
|
||||
tap.start();
|
||||
|
@ -1,9 +1,9 @@
|
||||
import * as plugins from './tink.plugins';
|
||||
|
||||
import { TinkUser } from './tink.classes.tinkuser'
|
||||
import { TinkUser } from './tink.classes.tinkuser';
|
||||
|
||||
export class TinkAccount {
|
||||
private clientId: string;
|
||||
public clientId: string;
|
||||
private clientSecret: string;
|
||||
|
||||
private apiBaseUrl: string = 'https://api.tink.com';
|
||||
@ -16,30 +16,20 @@ export class TinkAccount {
|
||||
public async getTinkHealthyBoolean(): Promise<boolean> {
|
||||
const response = await plugins.smartrequest.request(
|
||||
'https://api.tink.com/api/v1/monitoring/healthy',
|
||||
{}
|
||||
{
|
||||
keepAlive: false,
|
||||
}
|
||||
);
|
||||
return response.body === 'ok';
|
||||
}
|
||||
|
||||
// the request method for tink respecting platform specific stuff
|
||||
// e.g. certain headers if needed
|
||||
public async request(optionsArg: {
|
||||
urlArg: string,
|
||||
methodArg: 'POST' | 'GET',
|
||||
scopeArg: string,
|
||||
payloadArg: any,
|
||||
externalUserId: string
|
||||
}) {
|
||||
// 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...');
|
||||
}
|
||||
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`,
|
||||
{},
|
||||
{
|
||||
keepAlive: false,
|
||||
},
|
||||
[
|
||||
{
|
||||
key: 'client_id',
|
||||
@ -55,28 +45,134 @@ export class TinkAccount {
|
||||
},
|
||||
{
|
||||
key: 'scope',
|
||||
content: optionsArg.scopeArg,
|
||||
content: scopeArg,
|
||||
},
|
||||
]
|
||||
);
|
||||
if (response.statusCode !== 200) {
|
||||
console.log(response.statusCode);
|
||||
console.log(response.body);
|
||||
throw new Error('there was an error aquiring an access token.');
|
||||
}
|
||||
const accessToken = response.body.access_token;
|
||||
const clientAccessToken = response.body.access_token;
|
||||
return clientAccessToken;
|
||||
}
|
||||
|
||||
const response2 = await plugins.smartrequest.request(`${this.apiBaseUrl}${optionsArg.urlArg}`, {
|
||||
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`,
|
||||
{
|
||||
keepAlive: false,
|
||||
headers: {
|
||||
'Authorization': `Bearer ${accessToken}`,
|
||||
'Content-Type': 'application/json'
|
||||
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`,
|
||||
{
|
||||
keepAlive: false,
|
||||
},
|
||||
[
|
||||
{
|
||||
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}`, {
|
||||
keepAlive: false,
|
||||
headers: {
|
||||
Authorization: `Bearer ${optionsArg.accessToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method: optionsArg.methodArg,
|
||||
requestBody: JSON.stringify(optionsArg.payloadArg)
|
||||
})
|
||||
console.log(response2.statusCode);
|
||||
return response2.body;
|
||||
requestBody: JSON.stringify(optionsArg.payloadArg),
|
||||
});
|
||||
console.log(response.statusCode);
|
||||
return response.body;
|
||||
}
|
||||
|
||||
public async getTinkUser(externalUserIdArg: string) {
|
||||
const tinkuser = await TinkUser.getTinkUser(this, externalUserIdArg);
|
||||
return tinkuser;
|
||||
}
|
||||
|
||||
public async createTinkUser(externalUserIdArg: string) {
|
||||
const tinkuser = await TinkUser.createNewTinkUser(this, externalUserIdArg);
|
||||
return tinkuser;
|
||||
}
|
||||
}
|
||||
|
30
ts/tink.classes.tinkproviderconsent.ts
Normal file
30
ts/tink.classes.tinkproviderconsent.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import * as plugins from './tink.plugins';
|
||||
|
||||
import { TinkUser } from './tink.classes.tinkuser';
|
||||
|
||||
/**
|
||||
* a provider consent maps to tinks bank consents
|
||||
*/
|
||||
export class TinkProviderConsent {
|
||||
// STATIC
|
||||
public static async getProviderConsentsForUser(tinkUserRefArg: TinkUser) {
|
||||
const returnProviderConsents: TinkProviderConsent[] = [];
|
||||
const authorizationCode = await tinkUserRefArg.tinkAccountRef.getUserAuthorizationCode(
|
||||
tinkUserRefArg.externalUserIdArg,
|
||||
tinkUserRefArg.tinkAccountRef.clientId,
|
||||
'accounts:read,balances:read,transactions:read,provider-consents:read'
|
||||
);
|
||||
const accessToken = await tinkUserRefArg.tinkAccountRef.getUserAccessToken(authorizationCode);
|
||||
const responseData = await tinkUserRefArg.tinkAccountRef.request({
|
||||
urlArg: '/api/v1/provider-consents',
|
||||
accessToken,
|
||||
methodArg: 'GET',
|
||||
payloadArg: null
|
||||
})
|
||||
console.log(responseData);
|
||||
return returnProviderConsents;
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
constructor(tinkUserRefArg: TinkUser) {}
|
||||
}
|
@ -1,23 +1,78 @@
|
||||
import * as plugins from './tink.plugins';
|
||||
|
||||
import { TinkAccount } from './tink.classes.tinkaccount';
|
||||
import { TinkProviderConsent } from './tink.classes.tinkproviderconsent';
|
||||
|
||||
export class TinkUser {
|
||||
// STATIC
|
||||
public static async createNewTinkUser (tinkaccountArg: TinkAccount, externalUserIdArg: string) {
|
||||
const response = await tinkaccountArg.request({
|
||||
externalUserId: null,
|
||||
public static async createNewTinkUser(tinkAccountArg: TinkAccount, externalUserIdArg: string) {
|
||||
const accessToken = await tinkAccountArg.getClientAccessTokenForScope('user:create');
|
||||
const responseData: {
|
||||
external_user_id: string;
|
||||
user_id: string;
|
||||
} = await tinkAccountArg.request({
|
||||
urlArg: '/api/v1/user/create',
|
||||
accessToken,
|
||||
methodArg: 'POST',
|
||||
scopeArg: 'user:create',
|
||||
payloadArg: {
|
||||
"external_user_id": externalUserIdArg,
|
||||
"market": "GB",
|
||||
"locale": "en_US"
|
||||
}
|
||||
external_user_id: externalUserIdArg,
|
||||
market: 'DE',
|
||||
locale: 'en_US',
|
||||
},
|
||||
});
|
||||
|
||||
console.log(response);
|
||||
const newTinkUser = await TinkUser.getTinkUser(tinkAccountArg, externalUserIdArg);
|
||||
return newTinkUser;
|
||||
}
|
||||
|
||||
public static async getTinkUser(tinkAccountArg: TinkAccount, externalUserIdArg: string) {
|
||||
const authorizationCode = await tinkAccountArg.getUserAuthorizationCode(
|
||||
externalUserIdArg,
|
||||
tinkAccountArg.clientId,
|
||||
'user:read'
|
||||
);
|
||||
const accessToken = await tinkAccountArg.getUserAccessToken(authorizationCode);
|
||||
const responseData: {
|
||||
appId: string;
|
||||
created: string;
|
||||
externalUserId: string;
|
||||
flags: string[];
|
||||
id: string;
|
||||
nationalId: string;
|
||||
profile: {
|
||||
// cashbackEnabled: boolean; // deprecated
|
||||
currency: string;
|
||||
locale: string;
|
||||
market: string;
|
||||
notificationSettings: {
|
||||
balance: boolean;
|
||||
budget: boolean;
|
||||
doubleCharge: boolean;
|
||||
einvoices: boolean;
|
||||
fraud: boolean;
|
||||
income: boolean;
|
||||
largeExpense: boolean;
|
||||
leftToSpend: boolean;
|
||||
loanUpdate: boolean;
|
||||
summaryMonthly: boolean;
|
||||
summaryWeekly: boolean;
|
||||
transaction: boolean;
|
||||
unusualAccount: boolean;
|
||||
unusualCategory: boolean;
|
||||
};
|
||||
periodAdjustedDay: 25;
|
||||
periodMode: 'MONTHLY_ADJUSTED' | 'MONTHLY';
|
||||
timeZone: string;
|
||||
};
|
||||
// username: string; // not relevant
|
||||
} = await tinkAccountArg.request({
|
||||
urlArg: '/api/v1/user',
|
||||
accessToken,
|
||||
methodArg: 'GET',
|
||||
payloadArg: null,
|
||||
});
|
||||
const newTinkUser = new TinkUser(tinkAccountArg, responseData.id, responseData.externalUserId);
|
||||
return newTinkUser;
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
@ -25,7 +80,7 @@ export class TinkUser {
|
||||
public tinkUserId: string;
|
||||
public externalUserIdArg: string;
|
||||
|
||||
public authorizationToken: string
|
||||
public authorizationToken: string;
|
||||
|
||||
constructor(tinkAccountrefArg: TinkAccount, tinkUserIdArg: string, externalUserIdArg: string) {
|
||||
this.tinkAccountRef = tinkAccountrefArg;
|
||||
@ -33,21 +88,42 @@ export class TinkUser {
|
||||
this.externalUserIdArg = externalUserIdArg;
|
||||
}
|
||||
|
||||
public async authorize() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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({
|
||||
externalUserId: this.externalUserIdArg,
|
||||
methodArg: 'POST',
|
||||
accessToken,
|
||||
payloadArg: {},
|
||||
scopeArg: 'user:delete',
|
||||
urlArg: '/api/v1/user/delete'
|
||||
urlArg: '/api/v1/user/delete',
|
||||
});
|
||||
console.log(response);
|
||||
console.log(`successfully deleted user with externalId ${this.externalUserIdArg}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* gets a tink link that can be used by a user to connect accounts
|
||||
* @returns
|
||||
*/
|
||||
public async getTinkLinkForMarket(countryIdArg: string = 'DE'): 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=${countryIdArg}`;
|
||||
return tinkLinkUrl;
|
||||
}
|
||||
|
||||
public async getProviderConsents(): Promise<TinkProviderConsent[]> {
|
||||
const providerConsents: TinkProviderConsent[] =
|
||||
await TinkProviderConsent.getProviderConsentsForUser(this);
|
||||
return providerConsents;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user