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

Update oauth4webapi in gatekeeper #61

Merged
merged 1 commit into from
Oct 26, 2024
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
2 changes: 1 addition & 1 deletion gatekeeper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"dependencies": {
"itty-router": "^5.0.18",
"oauth4webapi": "^2.3.0"
"oauth4webapi": "^3.1.2"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20241018.0",
Expand Down
34 changes: 7 additions & 27 deletions gatekeeper/src/oauth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ class OauthClient {
#config: OauthClientConfig;
#as!: oauth.AuthorizationServer;
#client: oauth.Client;
#clientAuth: oauth.ClientAuth;

constructor(config: OauthClientConfig) {
this.#config = config;
this.#codeVerifier = oauth.generateRandomCodeVerifier();
this.#client = {
client_id: this.#config.clientId,
client_secret: this.#config.clientSecret
client_id: this.#config.clientId
};
this.#clientAuth = oauth.ClientSecretBasic(this.#config.clientSecret);
}

async readServer() {
Expand Down Expand Up @@ -50,53 +51,37 @@ class OauthClient {
return loginUrl;
}

async grantCode(url: string | URL): Promise<oauth.OAuth2TokenEndpointResponse> {
async grantCode(url: string | URL): Promise<oauth.TokenEndpointResponse> {
const params = oauth.validateAuthResponse(
this.#as,
this.#client,
typeof url === 'string' ? new URL(url) : url,
oauth.expectNoState
);

if (oauth.isOAuth2Error(params)) {
console.log('error', params);
throw new Error(); // Handle OAuth 2.0 redirect error
}

const response = await oauth.authorizationCodeGrantRequest(
this.#as,
this.#client,
this.#clientAuth,
params,
this.#config.grantRedirectURI,
this.#codeVerifier
);

let challenges: oauth.WWWAuthenticateChallenge[] | undefined;
if ((challenges = oauth.parseWwwAuthenticateChallenges(response))) {
for (const challenge of challenges) {
console.log('challenge', challenge);
}
throw new Error(); // Handle www-authenticate challenges as needed
}

const result = await oauth.processAuthorizationCodeOAuth2Response(
const result = await oauth.processAuthorizationCodeResponse(
this.#as,
this.#client,
response
);

if (oauth.isOAuth2Error(result)) {
console.error(result);
throw new Error(); // Handle OAuth 2.0 response body error
}

return result;
}

async refresh(token: string): Promise<oauth.TokenEndpointResponse> {
const response = await oauth.refreshTokenGrantRequest(
this.#as,
this.#client,
this.#clientAuth,
token
);

Expand All @@ -106,11 +91,6 @@ class OauthClient {
response
);

if (oauth.isOAuth2Error(result)) {
console.error(result);
throw new Error(); // Handle OAuth 2.0 response body error
}

return result;
}
}
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

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

Loading