|
| 1 | +/*! |
| 2 | + * Copyright 2023 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import * as validator from '../utils/validator'; |
| 17 | +import { AuthClientErrorCode, FirebaseAuthError } from '../utils/error'; |
| 18 | +import {deepCopy} from '../utils/deep-copy'; |
| 19 | + |
| 20 | +export interface PasskeyConfigRequest { |
| 21 | + expectedOrigins?: string[]; |
| 22 | +} |
| 23 | + |
| 24 | +export interface PasskeyConfigServerResponse { |
| 25 | + name?: string; |
| 26 | + rpId?: string; |
| 27 | + expectedOrigins?: string[]; |
| 28 | +} |
| 29 | + |
| 30 | +export interface PasskeyConfigClientRequest { |
| 31 | + rpId?: string; |
| 32 | + expectedOrigins?: string[]; |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +export class PasskeyConfig { |
| 37 | + public readonly name?: string; |
| 38 | + public readonly rpId?: string; |
| 39 | + public readonly expectedOrigins?: string[]; |
| 40 | + |
| 41 | + private static validate(isCreateRequest: boolean, passkeyConfigRequest?: PasskeyConfigRequest, rpId?: string) { |
| 42 | + if(isCreateRequest && !validator.isNonEmptyString(rpId)) { |
| 43 | + throw new FirebaseAuthError( |
| 44 | + AuthClientErrorCode.INVALID_ARGUMENT, |
| 45 | + `'rpId' must be a valid non-empty string'`, |
| 46 | + ); |
| 47 | + } |
| 48 | + if(!isCreateRequest && typeof rpId !== 'undefined') { |
| 49 | + throw new FirebaseAuthError( |
| 50 | + AuthClientErrorCode.INVALID_ARGUMENT, |
| 51 | + `'rpId' cannot be changed once created.'`, |
| 52 | + ); |
| 53 | + } |
| 54 | + if(!validator.isNonNullObject(passkeyConfigRequest)) { |
| 55 | + throw new FirebaseAuthError( |
| 56 | + AuthClientErrorCode.INVALID_ARGUMENT, |
| 57 | + `'passkeyConfigRequest' must be a valid non-empty object.'`, |
| 58 | + ); |
| 59 | + } |
| 60 | + const validKeys = { |
| 61 | + expectedOrigins: true, |
| 62 | + }; |
| 63 | + // Check for unsupported top level attributes. |
| 64 | + for (const key in passkeyConfigRequest) { |
| 65 | + if (!(key in validKeys)) { |
| 66 | + throw new FirebaseAuthError( |
| 67 | + AuthClientErrorCode.INVALID_ARGUMENT, |
| 68 | + `'${key}' is not a valid PasskeyConfigRequest parameter.`, |
| 69 | + ); |
| 70 | + } |
| 71 | + } |
| 72 | + if(!validator.isNonEmptyArray(passkeyConfigRequest.expectedOrigins) || !validator.isNonNullObject(passkeyConfigRequest.expectedOrigins)) { |
| 73 | + throw new FirebaseAuthError( |
| 74 | + AuthClientErrorCode.INVALID_ARGUMENT, |
| 75 | + `'passkeyConfigRequest.expectedOrigins' must be a valid non-empty array of strings.'`, |
| 76 | + ); |
| 77 | + } |
| 78 | + for(const origin in passkeyConfigRequest.expectedOrigins) { |
| 79 | + if(!validator.isString(origin)) { |
| 80 | + throw new FirebaseAuthError( |
| 81 | + AuthClientErrorCode.INVALID_ARGUMENT, |
| 82 | + `'passkeyConfigRequest.expectedOrigins' must be a valid non-empty array of strings.'`, |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + public static buildServerRequest(isCreateRequest: boolean, passkeyConfigRequest?: PasskeyConfigRequest, rpId?: string): PasskeyConfigClientRequest { |
| 89 | + PasskeyConfig.validate(isCreateRequest, passkeyConfigRequest, rpId); |
| 90 | + let request: PasskeyConfigClientRequest = {}; |
| 91 | + if(isCreateRequest && typeof rpId !== 'undefined') { |
| 92 | + request.rpId = rpId; |
| 93 | + } |
| 94 | + if(typeof request.expectedOrigins !== 'undefined') { |
| 95 | + request.expectedOrigins = passkeyConfigRequest?.expectedOrigins; |
| 96 | + } |
| 97 | + return request; |
| 98 | + }; |
| 99 | + |
| 100 | + constructor(response: PasskeyConfigServerResponse) { |
| 101 | + if(typeof response.name !== 'undefined') { |
| 102 | + this.name = response.name; |
| 103 | + } |
| 104 | + if(typeof response.rpId !== 'undefined') { |
| 105 | + this.rpId = response.rpId; |
| 106 | + }; |
| 107 | + if(typeof response.expectedOrigins !== 'undefined') { |
| 108 | + this.expectedOrigins = response.expectedOrigins; |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + public toJSON(): object { |
| 113 | + const json = { |
| 114 | + name: deepCopy(this.name), |
| 115 | + rpId: deepCopy(this.rpId), |
| 116 | + expectedOrigins: deepCopy(this.expectedOrigins), |
| 117 | + }; |
| 118 | + if(typeof json.name === 'undefined') { |
| 119 | + delete json.name; |
| 120 | + } |
| 121 | + if(typeof json.rpId === 'undefined') { |
| 122 | + delete json.rpId; |
| 123 | + } |
| 124 | + if(typeof json.expectedOrigins === 'undefined') { |
| 125 | + delete json.expectedOrigins; |
| 126 | + } |
| 127 | + return json; |
| 128 | + } |
| 129 | + |
| 130 | +}; |
| 131 | + |
0 commit comments