aboutsummaryrefslogtreecommitdiff
path: root/extension/src/features/auth-api.ts
blob: fbc9420780d8ece8624f0fe3119d2221939ea690 (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
187
188
189
190
191
192
193
194
195
// Copyright (C) 2024 Vaughn Nugent
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

import { AxiosInstance } from "axios";
import { get, useTimeoutFn, set } from "@vueuse/core";
import { computed, shallowRef } from "vue";
import { clone, defer, delay } from "lodash";
import { IMfaFlowContinuiation, totpMfaProcessor, useMfaLogin, usePkiAuth, useSession, useUser,
     type IMfaSubmission, type IMfaMessage, type WebMessage 
} from "@vnuge/vnlib.browser";
import { type FeatureApi, type BgRuntime, type IFeatureExport, exportForegroundApi, popupAndOptionsOnly, popupOnly } from "./framework";
import { waitForChangeFn } from "./util";
import type { ClientStatus } from "./types";
import type { AppSettings } from "./settings";
import type { JsonObject } from "type-fest";


export interface ProectedHandler<T extends JsonObject> {
    (message: T): Promise<any>
}

export interface MessageHandler<T extends JsonObject> {
    (message: T): Promise<any>
}

export interface ApiMessageHandler<T extends JsonObject> {
    (message: T, apiHandle: { axios: AxiosInstance }): Promise<any>
}

export interface UserApi extends FeatureApi {
    login(username: string, password?: string): Promise<boolean>
    logout: () => Promise<void>
    getProfile: () => Promise<any>
    getStatus: () => Promise<ClientStatus>
    waitForChange: () => Promise<void>
    submitMfa: (submission: IMfaSubmission) => Promise<boolean>
}

export const useAuthApi = (): IFeatureExport<AppSettings, UserApi> => {

    return {
        background: ({ state }:BgRuntime<AppSettings>): UserApi =>{
            const { loggedIn, clearLoginState } = useSession();
            const { currentConfig } = state
            const { logout, getProfile, heartbeat, userName } = useUser();
            const currentPkiPath = computed(() => `${currentConfig.value.accountBasePath}/pki`)
            
            //Use pki login controls
            const pkiAuth = usePkiAuth(currentPkiPath as any)
            const { login } = useMfaLogin([ totpMfaProcessor() ])

            //We can send post messages to the server heartbeat endpoint to get status
            const runHeartbeat = async () => {
                //Only run if the api thinks its logged in, and config is enabled
                if (!loggedIn.value || currentConfig.value.heartbeat !== true) {
                    return
                }

                try {
                    //Post against the heartbeat endpoint
                    await heartbeat()
                }
                catch (error: any) {
                    if (error.response?.status === 401 || error.response?.status === 403) {
                        //If we get a 401, the user is no longer logged in
                        clearLoginState()
                    }
                }
            }

            const mfaUpgrade = (() => {

                const store = shallowRef<IMfaFlowContinuiation | null>(null)
                const message = computed<IMfaMessage | null>(() =>{
                    if(!store.value){
                        return null
                    }
                    //clone the continuation to send to the popup
                    const cl = clone<Partial<IMfaFlowContinuiation>>(store.value)
                    //Remove the submit method from the continuation
                    delete cl.submit;
                    return cl as IMfaMessage
                })

                const { start, stop } = useTimeoutFn(() => set(store, null), 360 * 1000)

                return{
                    setContiuation(cont: IMfaFlowContinuiation){
                        //Store continuation for later
                        set(store, cont)
                        //Restart cleanup timer
                        start()
                    },
                    continuation: message,
                    async submit(submission: IMfaSubmission){
                        const cont = get(store)
                        if(!cont){
                            throw new Error('MFA login expired')
                        }
                        const response = await cont.submit(submission)
                        response.getResultOrThrow()

                        //Stop timer
                        stop()
                        //clear the continuation
                        defer(() => set(store, null))
                    }
                }
            })()

            //Configure interval to run every 5 minutes to update the status
            setInterval(runHeartbeat, 60 * 1000);
            delay(runHeartbeat, 1000)   //Delay 1 second to allow the extension to load

            return {
                waitForChange: waitForChangeFn([currentConfig, loggedIn, userName, mfaUpgrade.continuation]),
                login: popupOnly(async (usernameOrToken: string, password?: string): Promise<boolean> => {
                    
                    if(password){
                        const result = await login(usernameOrToken, password)
                        if ('getResultOrThrow' in result){
                            (result as WebMessage).getResultOrThrow()
                        }
                        
                        if((result as IMfaFlowContinuiation).submit){
                            //Capture continuation, store for submission for later, and set the continuation
                            mfaUpgrade.setContiuation(result as IMfaFlowContinuiation);
                            return true;
                        }

                        //Otherwise normal login
                    }
                    else{
                        //Perform login
                        await pkiAuth.login(usernameOrToken)
                    }
                  
                    //load profile
                    getProfile()
                    return true;
                }),
                logout: popupOnly(async (): Promise<void> => {
                    //Perform logout
                    await logout()
                    //Cleanup after logout
                    clearLoginState()
                }),
                submitMfa: popupOnly(async (submission: IMfaSubmission): Promise<boolean> => {
                    const cont = get(mfaUpgrade.continuation)
                    if(!cont || cont.expired){
                        return false;
                    }

                    //Submit the continuation
                    await mfaUpgrade.submit(submission);

                    //load profile
                    getProfile()
                    return true;
                }),
                getProfile: popupAndOptionsOnly(getProfile),
                async getStatus (){
                    return {
                        //Logged in if the cookie is set and the api flag is set
                        loggedIn: get(loggedIn),
                        //username
                        userName: get(userName),
                        //mfa status
                        mfaStatus: get(mfaUpgrade.continuation)
                    } as ClientStatus
                },
            }
        },
        foreground: exportForegroundApi<UserApi>([
            'login',
            'logout',
            'getProfile',
            'getStatus',
            'waitForChange',
            'submitMfa',
        ]),
    } 
}