-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cloud): encapsulate backend-specific configs (#6710)
* refactor(cloud): encapsulate backend-specific configs To make `AuthRedirectServer` backend-agnostic. Co-authored-by: Steffen Neubauer <[email protected]> * refactor: move backend-specific machinery to own ts module * chore: lint --------- Co-authored-by: Steffen Neubauer <[email protected]>
- Loading branch information
1 parent
62ddc3c
commit a5783bd
Showing
3 changed files
with
74 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (C) 2018-2024 Garden Technologies, Inc. <[email protected]> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import type { AuthRedirectServerConfig } from "./auth.js" | ||
import { isArray } from "lodash-es" | ||
|
||
function getFirstValue(v: string | string[]) { | ||
return isArray(v) ? v[0] : v | ||
} | ||
|
||
export type GardenBackendConfig = { readonly cloudDomain: string } | ||
export type AuthRedirectConfig = Pick<AuthRedirectServerConfig, "getLoginUrl" | "successUrl" | "extractAuthToken"> | ||
|
||
export abstract class AbstractGardenBackend { | ||
constructor(protected readonly config: GardenBackendConfig) {} | ||
|
||
abstract getAuthRedirectConfig(): AuthRedirectConfig | ||
} | ||
|
||
export class GardenCloudBackend extends AbstractGardenBackend { | ||
override getAuthRedirectConfig(): AuthRedirectConfig { | ||
return { | ||
getLoginUrl: (port) => new URL(`/clilogin/${port}`, this.config.cloudDomain).href, | ||
successUrl: new URL("/clilogin/success", this.config.cloudDomain).href, | ||
extractAuthToken: (query) => { | ||
const { jwt, rt, jwtval } = query | ||
// TODO: validate properly | ||
return { | ||
token: getFirstValue(jwt!), | ||
refreshToken: getFirstValue(rt!), | ||
tokenValidity: parseInt(getFirstValue(jwtval!), 10), | ||
} | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters