-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (94 loc) · 3.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const generateRandomStateOrNonce=require('./generateStateAndNonce');
const generateTokens=require('./generateTokens');
const clientData=require('./data/client.json');
const path = require('path');
const express = require('express');
const app = express();
const redirectUri='http://localhost:3000/return'
const generateClientAssertion=require('./GenerateAccessToken');
const { emitWarning } = require('process');
const ClientToken=retrieveTokenUsingClientAssertion(clientData.id,generateClientAssertion.token, '');
//const queryString = window.location.search;
app.use(express.static(path.join(__dirname, 'public')))
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/public/index.html`);
});
const state=generateRandomStateOrNonce();
const nonce=generateRandomStateOrNonce();
//when frontend calls /authorurl
// you will generate a url and return it
let verifier,challenge;
app.get('/auth_url', (req, res) => {
console.log('123103132')
const start=async function() {
[verifier,challenge]=await generateTokens.generateTokens();
if(verifier && challenge){
const url= buildAuthorizationUrl(clientData.id,challenge,redirectUri,state,nonce,clientData.allowedScopes)
res.redirect(url);
//console.log(res);
};
}
start();
//const responseUrl=new URL(res)
});
app.get('/return',(req,res)=>{
const code=req.query.code;
retrieveTokenUsingAuthorizationCode(clientData.id,redirectUri,verifier,code,generateClientAssertion.token);
});
app.listen(3000, () => {
console.log('Application listening on port 3000!');
});
function buildAuthorizationUrl(clientId, challenge, redirectUri, state, nonce, scopes) {
const search = {
client_id: clientId,
code_challenge: challenge,
code_challenge_method: 'S256',
redirect_uri: redirectUri,
scope: scopes.join(' '),
response_type: 'code',
nonce: nonce,
state: state
};
const searchString = Object.entries(search).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&');
return `https://secure.stitch.money/connect/authorize?${searchString}`;
}
async function retrieveTokenUsingClientAssertion(clientId, clientAssertion, scopes) {
const body = {
grant_type: 'client_credentials',
client_id: clientId,
scope: 'transactions',
audience: 'https://secure.stitch.money/connect/token',
client_assertion_type: 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
client_assertion: clientAssertion
};
// console.log(scopes);
const bodyString = Object.entries(body).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&');
const response = await fetch('https://secure.stitch.money/connect/token', {
method: 'post',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: bodyString,
});
const responseBody = await response.json();
// console.log('Tokens: ', responseBody);
return responseBody;
}
async function retrieveTokenUsingAuthorizationCode(clientId, redirectUri, verifier, code, clientAssertion) {
const body = {
grant_type: 'authorization_code',
client_id: clientId,
code: code,
redirect_uri: redirectUri,
code_verifier: verifier,
client_assertion: clientAssertion,
client_assertion_type: 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer'
}
const bodyString = Object.entries(body).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&');
const response = await fetch('https://secure.stitch.money/connect/token', {
method: 'post',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: bodyString,
});
const responseBody = await response.json();
console.log('Tokens: ', responseBody);
return responseBody;
}