-
Notifications
You must be signed in to change notification settings - Fork 1
/
solid.js
60 lines (55 loc) · 2.4 KB
/
solid.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
import fetch from 'node-fetch';
import {createDpopHeader, generateDpopKeyPair} from '@inrupt/solid-client-authn-core';
/**
* Generate a token and secret linked to the users account and WebID
* @param {String} email - User email for pod on the server
* @param {String} password - User password for pod on the server
* @param {String} credentialsUrl - Url from which user credentials can be requested
* @returns {(String, String)} - Tuple containing user id and secret linked to the users WebID
*/
export async function getToken(email, password, credentialsUrl) {
const response = await fetch(credentialsUrl, {
method: 'POST',
headers: {'content-type': 'application/json'},
body: JSON.stringify({email: email, password: password, name: 'extension-token'}),
});
const {id, secret} = await response.json();
return {id, secret}
}
/**
* Generate a temporary access token to make authenticated requests
* @param {String} id - User id linked to the users WebID
* @param {String} secret - User secret linked to the users WebID
* @param {String} tokenUrl - Url from which an access token can be requested from the server
* @returns {String, KeyPair} - Temporary Access Token and it's corresponding keypair
*/
export async function getAccessToken(id, secret, tokenUrl) {
const dpopKey = await generateDpopKeyPair();
const authString = `${encodeURIComponent(id)}:${encodeURIComponent(secret)}`;
const receive = await fetch(tokenUrl, {
method: 'POST',
headers: {
authorization: `Basic ${Buffer.from(authString).toString('base64')}`,
'content-type': 'application/x-www-form-urlencoded',
dpop: await createDpopHeader(tokenUrl, 'POST', dpopKey),
},
body: 'grant_type=client_credentials&scope=webid',
});
const {access_token: accessToken} = await receive.json();
return {accessToken, dpopKey};
}
/**
* Request the url from which access tokens can be requested on a server
* @param {String} url - Url/domain on which the server is hosted
* @returns {String} - Url from which access tokens can be requested
*/
export async function getTokenUrl(url) {
let requestUrl = url + ".well-known/openid-configuration"
const response = await fetch(requestUrl, {
method: 'GET',
headers: {
'content-type': 'application/json'
}
});
return (await (await response.json())).token_endpoint;
}