Skip to content

Commit

Permalink
oauth: allow using templates in OAuth response template
Browse files Browse the repository at this point in the history
airbyte has recently introduced new fields to their OAuth response
mappings, which need processing. In this case, we have AirTable which
requires the `expires_in` field of response to be added to `now()` and
rendered as an RFC3339 date to work. So I'm adding a condition here
which allows response map to be rendered using a template instead of
just jsonpointer.

In hindsight, we can probably just get rid of jsonpointers here and use
mustache templates for the responseMap since it can do the same job
anyway. For now I'm keeping both.
  • Loading branch information
mdibaiee committed Apr 19, 2023
1 parent 781c08e commit 2ea0271
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 9 additions & 0 deletions supabase/functions/_shared/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export const mustacheHelpers = {
return btoa(render(s));
}
},
now_plus: function(s: any) {
return (s: string, render: any) => {
const now = new Date();
const inputSeconds = parseInt(render(s))
const newDate = new Date(now + inputSeconds * 1000)

return newDate.toISOString()
}
}
};

export const compileTemplate = (template: string, data: any, connector_id: string) => {
Expand Down
6 changes: 5 additions & 1 deletion supabase/functions/oauth/access-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ export async function accessToken(req: Record<string, any>) {

let mappedData: Record<string, any> = {};
for (const key in accessTokenResponseMap) {
mappedData[key] = jsonpointer.get(responseData, accessTokenResponseMap[key]);
if (accessTokenResponseMap[key].startsWith('/')) {
mappedData[key] = jsonpointer.get(responseData, accessTokenResponseMap[key]);
} else {
mappedData[key] = compileTemplate(accessTokenResponseMap[key], responseData);
}
}

return new Response(JSON.stringify(mappedData), {
Expand Down

0 comments on commit 2ea0271

Please sign in to comment.