|
25 | 25 | https://github.com/solid/solid |
26 | 26 | */ |
27 | 27 | 'use strict' |
28 | | - |
29 | | -module.exports = ClientAuthOIDC |
| 28 | +const RelyingParty = require('oidc-rp') |
30 | 29 |
|
31 | 30 | class ClientAuthOIDC { |
| 31 | + constructor () { |
| 32 | + this.rp = null |
| 33 | + this.providerUri = null |
| 34 | + } |
| 35 | + |
| 36 | + keyByProvider () { |
| 37 | + return `oidc.rp.by-provider.${this.providerUri}` |
| 38 | + } |
| 39 | + |
| 40 | + keyByState () { |
| 41 | + return `oidc.rp.by-state.${this.state}` |
| 42 | + } |
| 43 | + |
| 44 | + get client () { |
| 45 | + // If one is already initialized, return it |
| 46 | + if (this.rp) { |
| 47 | + return this.rp |
| 48 | + } |
| 49 | + // Check to see if there's already a registered client in local storage |
| 50 | + if (this.providerUri) { |
| 51 | + return localStorage.getItem(this.keyByProvider()) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + set client (rp) { |
| 56 | + this.rp = rp |
| 57 | + localStorage.setItem(this.keyByProvider(), rp.serialize()) |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @private |
| 62 | + * @param providerUri {string} |
| 63 | + * @param [options={}] |
| 64 | + * @param [options.redirectUri] {string} Defaults to window.location.href |
| 65 | + * @param [options.scope='openid profile'] {string} |
| 66 | + * @param [options.store=localStorage] |
| 67 | + * @throws {TypeError} If providerUri is missing |
| 68 | + * @return {Promise<RelyingParty>} Registered RelyingParty client instance |
| 69 | + */ |
| 70 | + register (providerUri, options = {}) { |
| 71 | + return this.registerPublicClient(providerUri, options) |
| 72 | + .then(client => { |
| 73 | + this.rp = client |
| 74 | + localStorage.setItem('oidc.clients.'+providerUri, client.serialize()) |
| 75 | + this.client_id = client.registration.client_id |
| 76 | + console.log('registered client:', client) |
| 77 | + }) |
| 78 | + .catch(error => { |
| 79 | + console.error('Error while registering:', error) |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @private |
| 85 | + * @param providerUri {string} |
| 86 | + * @param [options={}] |
| 87 | + * @param [options.redirectUri] {string} Defaults to window.location.href |
| 88 | + * @param [options.scope='openid profile'] {string} |
| 89 | + * @param [options.store=localStorage] |
| 90 | + * @throws {TypeError} If providerUri is missing |
| 91 | + * @return {Promise<RelyingParty>} Registered RelyingParty client instance |
| 92 | + */ |
| 93 | + registerPublicClient (providerUri, options = {}) { |
| 94 | + if (!providerUri) { |
| 95 | + throw TypeError('Cannot register auth client, missing providerUri') |
| 96 | + } |
| 97 | + let redirectUri = options.redirectUri || window.location.href |
| 98 | + this.redirectUri = redirectUri |
| 99 | + let registration = { |
| 100 | + issuer: providerUri, |
| 101 | + grant_types: ['implicit'], |
| 102 | + redirect_uris: [ redirectUri ], |
| 103 | + response_types: ['id_token token'], |
| 104 | + scope: options.scope || 'openid profile' |
| 105 | + } |
| 106 | + let rpOptions = { |
| 107 | + defaults: { |
| 108 | + authenticate: { |
| 109 | + redirect_uri: redirectUri, |
| 110 | + response_type: 'id_token token' |
| 111 | + } |
| 112 | + }, |
| 113 | + store: options.store || localStorage |
| 114 | + } |
| 115 | + return RelyingParty |
| 116 | + .register(providerUri, registration, rpOptions) |
| 117 | + } |
32 | 118 | } |
| 119 | +module.exports = ClientAuthOIDC |
0 commit comments