forked from FormidableLabs/react-native-app-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (61 loc) · 2.25 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
import invariant from 'invariant';
import { NativeModules } from 'react-native';
const { RNAppAuth } = NativeModules;
export default class AppAuth {
constructor(config) {
invariant(typeof config.issuer === 'string', 'Config error: issuer must be a string');
invariant(typeof config.clientId === 'string', 'Config error: clientId must be a string');
invariant(typeof config.clientSecret === 'string', 'Config error: clientSecret must be a string');
invariant(typeof config.redirectUrl === 'string', 'Config error: redirectUrl must be a string');
this.config = { ...config };
}
getConfig() {
return this.config;
}
authorize(scopes) {
invariant(scopes && scopes.length, 'Scope error: please add at least one scope');
return RNAppAuth.authorize(
this.config.issuer,
this.config.redirectUrl,
this.config.clientId,
this.config.clientSecret,
scopes
);
}
refresh(refreshToken, scopes) {
invariant(refreshToken, 'Please pass in a refresh token');
invariant(scopes && scopes.length, 'Scope error: please add at least one scope');
return RNAppAuth.refresh(
this.config.issuer,
this.config.redirectUrl,
this.config.clientId,
this.config.clientSecret,
refreshToken,
scopes
);
}
async revokeToken(tokenToRevoke, sendClientId = false) {
invariant(tokenToRevoke, 'Please include the token to revoke');
const response = await fetch(`${this.config.issuer}/.well-known/openid-configuration`);
const openidConfig = await response.json();
invariant(
openidConfig.revocation_endpoint,
'The openid config does not specify a revocation endpoint'
);
/**
Identity Server insists on client_id being passed in the body,
but Google does not. According to the spec, Google is right
so defaulting to no client_id
https://tools.ietf.org/html/rfc7009#section-2.1
**/
return await fetch(openidConfig.revocation_endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `token=${tokenToRevoke}${sendClientId ? `&client_id=${this.config.clientId}` : ''}`
}).catch(error => {
throw new Error('Failed to revoke token', error);
});
}
}