aboutsummaryrefslogtreecommitdiff
path: root/lib/vnlib.browser/src/mfa/fido.ts
blob: 8d586168528bdae21111740c5eca2e93d0d3e796 (plain)
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
// Copyright (c) 2024 Vaughn Nugent
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import { get } from "@vueuse/core";
import { type MaybeRef } from "vue";
import { useAxiosInternal } from "../axios";
import type { WebMessage } from "../types";
import type { AxiosRequestConfig } from "axios";
import type { 
    IMfaFlowContinuiation, 
    IMfaMessage, 
    IMfaTypeProcessor, 
    MfaSumissionHandler 
} from "./login";
import { startRegistration } from "@simplewebauthn/browser";
import type { RegistrationResponseJSON, PublicKeyCredentialCreationOptionsJSON } from "@simplewebauthn/types";

export type IFidoServerOptions = PublicKeyCredentialCreationOptionsJSON

export interface IFidoRequestOptions{
    readonly password: string;
}

export interface IFidoDevice{
    readonly id: string;
    readonly name: string;
    readonly registered_at: number;
}


interface FidoRegistration{
    readonly id: string;
    readonly friendlyName: string;
    readonly publicKey?: string;
    readonly publicKeyAlgorithm: number;
    readonly clientDataJSON: string;
    readonly authenticatorData?: string;
    readonly attestationObject?: string;
}

export interface IFidoApi {
    /**
     * Gets fido credential options from the server for a currently logged-in user
     * @returns A promise that resolves to the server options for the FIDO API
     */
    beginRegistration: (options?: Partial<IFidoRequestOptions>) => Promise<PublicKeyCredentialCreationOptionsJSON>;

    /**
     * Creates a new credential for the currently logged-in user
     * @param credential The credential to create
     * @returns A promise that resolves to a web message
     */
    registerCredential: (credential: RegistrationResponseJSON, commonName: string) => Promise<WebMessage>;
    
    /**
     * Registers the default device for the currently logged-in user
     * @returns A promise that resolves to a web message status of the operation
     */
    registerDefaultDevice: (commonName: string, options?: Partial<IFidoRequestOptions>) => Promise<WebMessage>;

    /**
     * Lists all devices for the currently logged-in user
     * @returns A promise that resolves to a list of devices
     */
    listDevices: () => Promise<IFidoDevice[]>;

    /**
     * Disables a device for the currently logged-in user.
     * May require a password to be passed in the options
     * @param device The device descriptor to disable
     * @param options The options to pass to the server
     * @returns A promise that resolves to a web message status of the operation
     */
    disableDevice: (device: IFidoDevice, options?: Partial<IFidoRequestOptions>) => Promise<WebMessage>;

    /**
     * Disables all devices for the currently logged-in user.
     * May require a password to be passed in the options
     * @param options The options to pass to the server
     * @returns A promise that resolves to a web message status of the operation
     */
    disableAllDevices: (options?: Partial<IFidoRequestOptions>) => Promise<WebMessage>;
}

/**
 * Creates a fido api for configuration and management of fido client devices
 * @param endpoint The fido server endpoint
 * @param axiosConfig The optional axios configuration to use
 * @returns An object containing the fido api
 */
export const useFidoApi = (endpoint: MaybeRef<string>, axiosConfig?: MaybeRef<AxiosRequestConfig | undefined | null>)
    : IFidoApi =>{
    const ep = () => get(endpoint);
    
    const axios = useAxiosInternal(axiosConfig)

    const beginRegistration = async (options?: Partial<IFidoRequestOptions>) : Promise<IFidoServerOptions> => {
        const { data } = await axios.value.put<WebMessage<IFidoServerOptions>>(ep(), options);
        return data.getResultOrThrow();
    }

    const registerCredential = async (reg: RegistrationResponseJSON, commonName: string): Promise<WebMessage> => {

        const registration: FidoRegistration = {
            id: reg.id,
            publicKey: reg.response.publicKey,
            publicKeyAlgorithm: reg.response.publicKeyAlgorithm!,
            clientDataJSON: reg.response.clientDataJSON,
            authenticatorData: reg.response.authenticatorData,
            attestationObject: reg.response.attestationObject,
            friendlyName: commonName
        }

        const { data } = await axios.value.post<WebMessage>(ep(), { registration });
        return data;
    }

    const registerDefaultDevice = async (commonName: string, options?: Partial<IFidoRequestOptions>): Promise<WebMessage> => {
        //begin registration
        const serverOptions = await beginRegistration(options);

        const reg = await startRegistration(serverOptions);
    
        return await registerCredential(reg, commonName);
    }

    const listDevices = async (): Promise<IFidoDevice[]> => {
        const { data } = await axios.value.get<WebMessage<IFidoDevice[]>>(ep());
        return data.getResultOrThrow();
    }

    const disableDevice = async (device: IFidoDevice, options?: Partial<IFidoRequestOptions>): Promise<WebMessage> => {
        const { data } = await axios.value.post<WebMessage>(ep(), { delete: device, ...options });
        return data;
    }

    const disableAllDevices = async (options?: Partial<IFidoRequestOptions>): Promise<WebMessage> => {
        const { data } = await axios.value.post<WebMessage>(ep(), options);
        return data;
    }

    return {
        beginRegistration,
        registerCredential,
        registerDefaultDevice,
        listDevices,
        disableDevice,
        disableAllDevices
    }
}

/**
 * Enables fido as a supported multi-factor authentication method
 * @returns A mfa login processor for fido multi-factor
 */
export const fidoMfaProcessor = () : IMfaTypeProcessor => {
    
    const processMfa = (payload: IMfaMessage, onSubmit: MfaSumissionHandler) : Promise<IMfaFlowContinuiation> => {

        return Promise.resolve({
            ...payload,
            submit: onSubmit.submit,
        })
    }

    return{
        type: "fido",
        processMfa
    }
}