Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve 'DeviceFlowHandler' configurability and update defaults #481

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/green-windows-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@monokle/synchronizer": minor
---

Improve 'DeviceFlowHandler' configurability
7 changes: 5 additions & 2 deletions packages/synchronizer/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ export const DEFAULT_STORAGE_CONFIG_FILE_AUTH = 'auth.yaml';

export const DEFAULT_API_URL = 'https://api.monokle.com';

export const DEFAULT_DEVICE_FLOW_CLIENT_ID = 'monokle';
export const DEFAULT_DEVICE_FLOW_IDP_URL = 'https://api.dev.monokle.com/identity';
export const DEFAULT_DEVICE_FLOW_IDP_URL = 'https://id.dev.monokle.com/realms/monokle';
export const DEFAULT_DEVICE_FLOW_CLIENT_ID = 'mc-cli';
export const DEFAULT_DEVICE_FLOW_CLIENT_SECRET = '';
export const DEFAULT_DEVICE_FLOW_ALG = 'ES512';
export const DEFAULT_DEVICE_FLOW_CLIENT_SCOPE = 'openid profile offline_access';
19 changes: 10 additions & 9 deletions packages/synchronizer/src/handlers/deviceFlowHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Issuer} from 'openid-client';
import {DEFAULT_DEVICE_FLOW_IDP_URL, DEFAULT_DEVICE_FLOW_CLIENT_ID} from '../constants.js';
import type {BaseClient, DeviceFlowHandle as DeviceFlowHandleOpenId, TokenSet as TokenSetOpenId} from 'openid-client';
import {DEFAULT_DEVICE_FLOW_IDP_URL, DEFAULT_DEVICE_FLOW_CLIENT_ID, DEFAULT_DEVICE_FLOW_CLIENT_SECRET, DEFAULT_DEVICE_FLOW_ALG, DEFAULT_DEVICE_FLOW_CLIENT_SCOPE} from '../constants.js';
import type {BaseClient, ClientMetadata, DeviceFlowHandle as DeviceFlowHandleOpenId, TokenSet as TokenSetOpenId} from 'openid-client';

export type DeviceFlowHandle = DeviceFlowHandleOpenId<BaseClient>;

Expand All @@ -11,14 +11,19 @@ export class DeviceFlowHandler {

constructor(
private _idpUrl: string = DEFAULT_DEVICE_FLOW_IDP_URL,
private _clientId: string = DEFAULT_DEVICE_FLOW_CLIENT_ID
private _clientMetadata: ClientMetadata = {
client_id: DEFAULT_DEVICE_FLOW_CLIENT_ID,
client_secret: DEFAULT_DEVICE_FLOW_CLIENT_SECRET,
id_token_signed_response_alg: DEFAULT_DEVICE_FLOW_ALG,
},
private _clientScope: string = DEFAULT_DEVICE_FLOW_CLIENT_SCOPE,
) {}

async initializeAuthFlow(): Promise<DeviceFlowHandle> {
const client = await this.getClient();

return client.deviceAuthorization({
scope: 'openid profile offline_access',
scope: this._clientScope,
});
}

Expand All @@ -34,11 +39,7 @@ export class DeviceFlowHandler {
private async getClient(): Promise<BaseClient> {
if (!this._currentClient) {
const monokleIssuer = await Issuer.discover(this._idpUrl);

this._currentClient = new monokleIssuer.Client({
client_id: this._clientId,
client_secret: '',
});
this._currentClient = new monokleIssuer.Client(this._clientMetadata);
}

return this._currentClient;
Expand Down
Loading