-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
192 lines (166 loc) · 7.15 KB
/
app.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { revokeToken } from './pwless-client/cognito-api.js';
import { scheduleRefresh, refreshTokens } from './pwless-client/refresh.js';
import { Passwordless } from './pwless-client/index.js';
import { requestSignInLink, signInWithLink } from './pwless-client/magic-link.js';
import { retrieveTokens, storeTokens } from './pwless-client/storage.js';
import { configure } from './pwless-client/config.js';
import { fido2CreateCredential, fido2ListCredentials, fido2DeleteCredential, authenticateWithFido2 } from './pwless-client/fido2.js';
import { parseJwtPayload } from "./pwless-client/util.js";
const clientId = '<clientid>';
const fido2BaseUrl = '<api gw endpoint>';
Passwordless.configure({
cognitoIdpEndpoint: 'eu-west-1',
clientId: clientId,
fido2: {
baseUrl: fido2BaseUrl,
authenticatorSelection: {
userVerification: "required",
},
},
debug: console.debug,
})
async function isLoggedInOrNewLogin() {
var tokens = await retrieveTokens();
const { clientId, debug, storage } = configure();
// If accesstoken etc is expired try to refresh from refreshtoken
if (tokens?.refreshToken && tokens?.expireAt <= Date.now()) {
refreshFromRefreshToken();
}
if (tokens && tokens?.expireAt >= Date.now()) {
scheduleTokenRefresh();
return true;
}
if (storage.getItem(`Passwordless.${clientId}.fido`)) {
document.getElementById('pkLoginLabel').innerHTML = `Login with Passkey ${JSON.parse(storage.getItem(`Passwordless.${clientId}.fido`)).email}`;
document.getElementById('pkLoginDialog').show();
} else {
await signInWithLink();
}
}
function scheduleTokenRefresh() {
scheduleRefresh({
tokensCb: (newTokens) =>
newTokens &&
storeTokens(newTokens)
})
}
async function refreshFromRefreshToken() {
const { clientId, debug, storage } = configure();
const tokens = await retrieveTokens();
try {
await refreshTokens({
tokensCb: (newTokens) =>
newTokens &&
storeTokens(newTokens)
})
} catch(err) {
if (err == 'NotAuthorizedException: Refresh Token has expired') {
console.log('Removing expired refresh token');
storage.removeItem(`CognitoIdentityServiceProvider.${clientId}.${tokens.username}.refreshToken`);
}
}
}
function loginSubmitHandler(evt) {
var authenticationData = {
email: document.getElementById("emailfield").value,
};
const { signInLinkRequested } = requestSignInLink({
usernameOrAlias: authenticationData.email,
statusCb: console.log
});
}
async function signOutHandler() {
console.log("Signing out");
const { clientId, debug, storage } = configure();
const tokens = await retrieveTokens();
storage.removeItem(`CognitoIdentityServiceProvider.${clientId}.${tokens.username}.accessToken`);
storage.removeItem(`CognitoIdentityServiceProvider.${clientId}.${tokens.username}.idToken`);
storage.removeItem(`CognitoIdentityServiceProvider.${clientId}.${tokens.username}.refreshToken`);
storage.removeItem(`CognitoIdentityServiceProvider.${clientId}.${tokens.username}.tokenScopesString`);
storage.removeItem(`CognitoIdentityServiceProvider.${clientId}.${tokens.username}.userData`);
storage.removeItem(`CognitoIdentityServiceProvider.${clientId}.LastAuthUser`);
storage.removeItem(`Passwordless.${clientId}.${tokens.username}.expireAt`);
storage.removeItem(`Passwordless.${clientId}.${tokens.username}.refreshTokens`);
await revokeToken({
abort: undefined, // if we've come this far, let this proceed
refreshToken: tokens.refreshToken,
});
document.location.reload()
}
async function showPasskeys() {
const credentials = await fido2ListCredentials();
const select = document.getElementById('selectPasskey');
for (var authenticator of credentials.authenticators) {
let element = document.createElement('option');
element.textContent = `${authenticator.friendlyName}`;
select.append(element);
}
const dialog = document.getElementById('passkeysDialog')
addInputFieldToParrent('Passkey Name', dialog);
addButtonToParrent('Create Passkey', dialog);
addButtonToParrent('Back', dialog);
const passform = select.parentElement;
addButtonToParrent('Delete', passform);
document.getElementById('loggedInDialog').close();
document.getElementById('passkeysDialog').show();
document.getElementById('CreatePasskey').addEventListener('click', createPasskey);
document.getElementById('Delete').addEventListener('click', deletePasskey);
}
function addButtonToParrent(btnName, parent) {
let button = document.createElement('button');
button.setAttribute('class', 'btn');
button.setAttribute('id', btnName.replaceAll(' ',''));
button.textContent = btnName;
parent.append(button);
}
function addInputFieldToParrent(inputName, parent) {
let input = document.createElement('input');
input.setAttribute('type', 'text');
input.setAttribute('id', inputName.replaceAll(' ',''));
input.setAttribute('name', inputName.replaceAll(' ',''));
input.textContent = inputName;
parent.append(input);
}
async function createPasskey() {
const { clientId, debug, storage } = configure();
console.log(document.getElementById('PasskeyName').value);
let fidoCredential = await fido2CreateCredential({ friendlyName: document.getElementById('PasskeyName').value});
const tokens = await retrieveTokens();
const { sub, email, "cognito:username": username, } = parseJwtPayload(tokens.idToken);
storage.setItem(`Passwordless.${clientId}.fido`, JSON.stringify({
username: username,
email: email,
credential: fidoCredential
}));
}
async function deletePasskey() {
const select = document.getElementById('selectPasskey');
const selected = select.options[select.selectedIndex].text;
const authenticators = (await fido2ListCredentials()).authenticators;
for (let authenticator of authenticators) {
if (authenticator.friendlyName == selected) {
fido2DeleteCredential({ credentialId: authenticator.credentialId });
}
}
}
async function authenticateWithPasskey() {
const { clientId, debug, storage } = configure();
const signingIn = authenticateWithFido2({
username: JSON.parse(storage.getItem(`Passwordless.${clientId}.fido`)).username,
});
signingIn.signedIn.then(() => { document.location.reload(); });
}
document.getElementById('loginForm').addEventListener('submit', loginSubmitHandler);
document.getElementById('loggedInButton').addEventListener('click', signOutHandler);
document.getElementById('passkeysButton').addEventListener('click', showPasskeys);
document.getElementById('pkLoginButton').addEventListener('click', authenticateWithPasskey);
if (await isLoggedInOrNewLogin()) {
document.getElementById('loginDialog').close();
document.getElementById('loggedInDialog').show();
} else {
document.getElementById('loggedInDialog').close();
if (! document.getElementById('pkLoginDialog').open) {
document.getElementById('loginDialog').show();
}
}
console.log(`Login status: ${await isLoggedInOrNewLogin()}`);