-
Notifications
You must be signed in to change notification settings - Fork 20
Figma oauth #1339
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
base: main
Are you sure you want to change the base?
Figma oauth #1339
Changes from all commits
fcb1fb2
c6c217d
3a068e2
083c3d1
290bb61
e6294a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,84 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { AppContext } from "../../mod.ts"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| interface OAuthCallbackResponse { | ||||||||||||||||||||||||||||||||||||||||||||||||
| access_token: string; | ||||||||||||||||||||||||||||||||||||||||||||||||
| expires_in: number; | ||||||||||||||||||||||||||||||||||||||||||||||||
| refresh_token: string; | ||||||||||||||||||||||||||||||||||||||||||||||||
| scope: string; | ||||||||||||||||||||||||||||||||||||||||||||||||
| token_type: string; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| export interface Props { | ||||||||||||||||||||||||||||||||||||||||||||||||
| code: string; | ||||||||||||||||||||||||||||||||||||||||||||||||
| installId: string; | ||||||||||||||||||||||||||||||||||||||||||||||||
| clientId: string; | ||||||||||||||||||||||||||||||||||||||||||||||||
| clientSecret: string; | ||||||||||||||||||||||||||||||||||||||||||||||||
| redirectUri: string; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||
| * @name OAUTH_CALLBACK | ||||||||||||||||||||||||||||||||||||||||||||||||
| * @title OAuth Callback | ||||||||||||||||||||||||||||||||||||||||||||||||
| * @description Exchanges the authorization code for access tokens | ||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||
| export default async function callback( | ||||||||||||||||||||||||||||||||||||||||||||||||
| { code, installId, clientId, clientSecret, redirectUri }: Props, | ||||||||||||||||||||||||||||||||||||||||||||||||
| req: Request, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ctx: AppContext, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ): Promise<{ installId: string; account?: string }> { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const { client } = ctx; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const finalRedirectUri = redirectUri || | ||||||||||||||||||||||||||||||||||||||||||||||||
| new URL("/oauth/callback", req.url).href; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const credentials = btoa(`${clientId}:${clientSecret}`); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const response = await client["POST /token"]({}, { | ||||||||||||||||||||||||||||||||||||||||||||||||
| body: { | ||||||||||||||||||||||||||||||||||||||||||||||||
| code, | ||||||||||||||||||||||||||||||||||||||||||||||||
| redirect_uri: finalRedirectUri, | ||||||||||||||||||||||||||||||||||||||||||||||||
| grant_type: "authorization_code", | ||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||
| headers: { | ||||||||||||||||||||||||||||||||||||||||||||||||
| "Authorization": `Basic ${credentials}`, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+37
to
+46
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Increase token endpoint compatibility Include client_id and client_secret in the body and set Accept; some providers require form-like params or both Basic and body credentials. - const response = await client["POST /token"]({}, {
- body: {
- code,
- redirect_uri: finalRedirectUri,
- grant_type: "authorization_code",
- },
- headers: {
- "Authorization": `Basic ${credentials}`,
- },
- });
+ const response = await client["POST /token"]({}, {
+ body: {
+ code,
+ redirect_uri: finalRedirectUri,
+ grant_type: "authorization_code",
+ client_id: clientId,
+ client_secret: clientSecret,
+ },
+ headers: {
+ "Authorization": `Basic ${credentials}`,
+ "Accept": "application/json",
+ },
+ });📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (!response.ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const errorText = await response.text(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error(`Token exchange failed: ${response.status} ${errorText}`); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const tokenData = await response.json() as OAuthCallbackResponse; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const currentTime = Math.floor(Date.now() / 1000); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const currentCtx = await ctx.getConfiguration(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| await ctx.configure({ | ||||||||||||||||||||||||||||||||||||||||||||||||
| ...currentCtx, | ||||||||||||||||||||||||||||||||||||||||||||||||
| tokens: { | ||||||||||||||||||||||||||||||||||||||||||||||||
| access_token: tokenData.access_token, | ||||||||||||||||||||||||||||||||||||||||||||||||
| refresh_token: tokenData.refresh_token, | ||||||||||||||||||||||||||||||||||||||||||||||||
| expires_in: tokenData.expires_in, | ||||||||||||||||||||||||||||||||||||||||||||||||
| scope: tokenData.scope, | ||||||||||||||||||||||||||||||||||||||||||||||||
| token_type: tokenData.token_type, | ||||||||||||||||||||||||||||||||||||||||||||||||
| tokenObtainedAt: currentTime, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||
| clientSecret: clientSecret, | ||||||||||||||||||||||||||||||||||||||||||||||||
| clientId: clientId, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const account = await ctx.invoke["figma"].loaders.oauth.whoami({ | ||||||||||||||||||||||||||||||||||||||||||||||||
| accessToken: tokenData.access_token, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .then((user: { email: string; handle: string }) => | ||||||||||||||||||||||||||||||||||||||||||||||||
| user.email || user.handle | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .catch(console.error) || undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return { installId, account }; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (_error) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return { installId, account: "error oauth" }; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Security: validate OAuth state
There’s no state verification, which weakens CSRF protections. Validate the state returned on the callback against the one issued in start.
Example patch:
Also consider adding state to Props or retrieving expected state from ctx.
📝 Committable suggestion