Skip to content

Commit 0bf7124

Browse files
feat: Canva OAuth Configuration (#337)
1 parent a4a628d commit 0bf7124

File tree

5 files changed

+230
-1
lines changed

5 files changed

+230
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import axios from 'axios';
2+
import { DataObject, OAuthResponse } from '../../lib/types';
3+
import qs from 'qs';
4+
import 'dotenv/config';
5+
import { base64UrlEncode } from '../../lib/helpers';
6+
7+
export const init = async ({ body }: DataObject): Promise<OAuthResponse> => {
8+
try {
9+
const {
10+
clientId: client_id,
11+
clientSecret: client_secret,
12+
metadata: { code, redirectUri: redirect_uri } = {},
13+
} = body;
14+
15+
const data = qs.stringify({
16+
code,
17+
redirect_uri,
18+
grant_type: 'authorization_code',
19+
code_verifier: process.env.CANVA_CODE_VERIFIER,
20+
});
21+
22+
const authorizationToken = await base64UrlEncode(
23+
`${client_id}:${client_secret}`,
24+
);
25+
26+
const response = await axios({
27+
url: 'https://api.canva.com/rest/v1/oauth/token',
28+
method: 'POST',
29+
headers: {
30+
Accept: 'application/json',
31+
'Content-Type': 'application/x-www-form-urlencoded',
32+
Authorization: `Basic ${authorizationToken}`,
33+
},
34+
data,
35+
});
36+
37+
const {
38+
data: {
39+
access_token: accessToken,
40+
refresh_token: refreshToken,
41+
token_type: tokenType,
42+
expires_in: expiresIn,
43+
},
44+
} = response;
45+
46+
return {
47+
accessToken,
48+
refreshToken,
49+
expiresIn,
50+
tokenType: tokenType === 'bearer' ? 'Bearer' : tokenType,
51+
meta: {},
52+
};
53+
} catch (error) {
54+
throw new Error(`Error fetching access token for Canva: ${error}`);
55+
}
56+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { DataObject, OAuthResponse } from '../../lib/types';
2+
import axios from 'axios';
3+
import { base64UrlEncode } from '../../lib/helpers';
4+
import qs from 'qs';
5+
6+
export const refresh = async ({ body }: DataObject): Promise<OAuthResponse> => {
7+
try {
8+
const {
9+
OAUTH_CLIENT_ID: client_id,
10+
OAUTH_CLIENT_SECRET: client_secret,
11+
OAUTH_REFRESH_TOKEN: refresh_token,
12+
} = body;
13+
14+
const data = qs.stringify({
15+
refresh_token,
16+
grant_type: 'refresh_token',
17+
});
18+
19+
const authorizationToken = await base64UrlEncode(
20+
`${client_id}:${client_secret}`,
21+
);
22+
23+
const response = await axios({
24+
url: 'https://api.canva.com/rest/v1/oauth/token',
25+
method: 'POST',
26+
headers: {
27+
Accept: 'application/json',
28+
'Content-Type': 'application/x-www-form-urlencoded',
29+
Authorization: `Basic ${authorizationToken}`,
30+
},
31+
data,
32+
});
33+
34+
const {
35+
data: {
36+
access_token: accessToken,
37+
refresh_token: refreshToken,
38+
token_type: tokenType,
39+
expires_in: expiresIn,
40+
},
41+
} = response;
42+
43+
return {
44+
accessToken,
45+
refreshToken,
46+
expiresIn,
47+
tokenType,
48+
meta: {},
49+
};
50+
} catch (error) {
51+
throw new Error(`Error fetching refresh token for Canva: ${error}`);
52+
}
53+
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import axios from 'axios';
2+
import qs from 'qs';
3+
import { DataObject, OAuthResponse } from '../../lib/types';
4+
5+
export const init = async ({ body }: DataObject): Promise<OAuthResponse> => {
6+
try {
7+
const {
8+
clientId: client_id,
9+
clientSecret: client_secret,
10+
metadata: {
11+
code,
12+
redirectUri: redirect_uri,
13+
formData: { CONTENTSTACK_CONTENT_MANAGEMENT_STACK_API_KEY },
14+
},
15+
} = body;
16+
17+
const requestBody = {
18+
grant_type: 'authorization_code',
19+
code,
20+
client_id,
21+
client_secret,
22+
redirect_uri,
23+
};
24+
25+
const response = await axios({
26+
url: 'https://app.contentstack.com/apps-api/token',
27+
method: 'POST',
28+
headers: {
29+
'Content-Type': 'application/x-www-form-urlencoded',
30+
Accept: 'application/json',
31+
},
32+
data: qs.stringify(requestBody),
33+
});
34+
35+
const {
36+
access_token: accessToken,
37+
refresh_token: refreshToken,
38+
token_type: tokenType,
39+
expires_in: expiresIn,
40+
organization_uid: organizationUid,
41+
} = response.data;
42+
43+
await axios({
44+
url: 'https://api.contentstack.io/v3/stacks/settings',
45+
method: 'GET',
46+
headers: {
47+
'Content-Type': 'application/json',
48+
Authorization: `Bearer ${accessToken}`,
49+
api_key: CONTENTSTACK_CONTENT_MANAGEMENT_STACK_API_KEY,
50+
},
51+
});
52+
53+
return {
54+
accessToken,
55+
refreshToken,
56+
expiresIn,
57+
tokenType: tokenType === 'bearer' ? 'Bearer' : tokenType,
58+
meta: {
59+
organizationUid,
60+
stackApiKey: CONTENTSTACK_CONTENT_MANAGEMENT_STACK_API_KEY,
61+
},
62+
};
63+
} catch (error) {
64+
throw new Error(
65+
`Error fetching access token for Contentstack Content Management: ${error}`,
66+
);
67+
}
68+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import axios from 'axios';
2+
import qs from 'qs';
3+
import { DataObject, OAuthResponse } from '../../lib/types';
4+
5+
export const refresh = async ({ body }: DataObject): Promise<OAuthResponse> => {
6+
try {
7+
const {
8+
OAUTH_CLIENT_ID: client_id,
9+
OAUTH_CLIENT_SECRET: client_secret,
10+
OAUTH_REFRESH_TOKEN: refresh_token,
11+
OAUTH_METADATA: { meta },
12+
OAUTH_REQUEST_PAYLOAD: { redirectUri: redirect_uri },
13+
} = body;
14+
15+
const requestBody = {
16+
grant_type: 'refresh_token',
17+
refresh_token,
18+
client_id,
19+
client_secret,
20+
redirect_uri,
21+
};
22+
23+
const response = await axios({
24+
url: 'https://app.contentstack.com/apps-api/token',
25+
method: 'POST',
26+
headers: {
27+
'Content-Type': 'application/x-www-form-urlencoded',
28+
Accept: 'application/json',
29+
},
30+
data: qs.stringify(requestBody),
31+
});
32+
33+
const {
34+
access_token: accessToken,
35+
refresh_token: refreshToken,
36+
token_type: tokenType,
37+
expires_in: expiresIn,
38+
} = response.data;
39+
40+
return {
41+
accessToken,
42+
refreshToken,
43+
expiresIn,
44+
tokenType: tokenType === 'bearer' ? 'Bearer' : tokenType,
45+
meta,
46+
};
47+
} catch (error) {
48+
throw new Error(
49+
`Error fetching access token for Contentstack Content Management: ${error}`,
50+
);
51+
}
52+
};

oauth/src/connections/zoho/refresh.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const refresh = async ({ body }: DataObject): Promise<OAuthResponse> => {
2626
},
2727
} = response;
2828

29-
// Update refresh token if a new token is allocated
29+
// Update the refresh token if a new token is allocated
3030
if (response.data.refresh_token) {
3131
refreshToken = response.data.refresh_token;
3232
}

0 commit comments

Comments
 (0)