Skip to content
Closed
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
98 changes: 31 additions & 67 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"js-yaml": "^4.1.0",
"jsonpath-plus": "^10.0.0",
"node-fetch": "^2.6.9",
"openid-client": "^5.6.5",
"openid-client": "^6.0.0",
"rfc4648": "^1.3.0",
"stream-buffers": "^3.0.2",
"tar": "^7.0.0",
Expand Down
37 changes: 30 additions & 7 deletions src/oidc_auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import https from 'node:https';
import { Client, Issuer } from 'openid-client';
import * as oidc from 'openid-client';
import { base64url } from 'rfc4648';

import { Authenticator } from './auth';
Expand All @@ -11,6 +11,29 @@ interface JwtObj {
signature: string;
}

interface Token {
id_token: string;
refresh_token: string;
expires_at: number;
}

interface Client {
refresh(token: string): Promise<Token>;
}

class OidcClient implements Client {
public constructor(readonly config: oidc.Configuration) {}

public async refresh(token: string): Promise<Token> {
const newToken = await oidc.refreshTokenGrant(this.config, token);
return {
id_token: newToken.id_token,
refresh_token: newToken.refresh_token,
expires_at: newToken.expiresIn(),
} as Token;
}
}

export class OpenIDConnectAuth implements Authenticator {
public static decodeJWT(token: string): JwtObj | null {
const parts = token.split('.');
Expand Down Expand Up @@ -95,16 +118,16 @@ export class OpenIDConnectAuth implements Authenticator {
const newToken = await client.refresh(user.authProvider.config['refresh-token']);
user.authProvider.config['id-token'] = newToken.id_token;
user.authProvider.config['refresh-token'] = newToken.refresh_token;
this.currentTokenExpiration = newToken.expires_at || 0;
this.currentTokenExpiration = newToken.expires_at;
}
return user.authProvider.config['id-token'];
}

private async getClient(user: User): Promise<Client> {
const oidcIssuer = await Issuer.discover(user.authProvider.config['idp-issuer-url']);
return new oidcIssuer.Client({
client_id: user.authProvider.config['client-id'],
client_secret: user.authProvider.config['client-secret'],
});
const configuration = await oidc.discovery(
user.authProvider.config['idp-issuer-url'],
user.authProvider.config['client-id'],
);
return new OidcClient(configuration);
}
}
Loading