Skip to content

Commit 98bf20d

Browse files
feat: Puzzle.io OAuth Configuration (#332)
1 parent e80f74d commit 98bf20d

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import axios from 'axios';
2+
import { DataObject, OAuthResponse } from '../../lib/types';
3+
4+
export const init = async ({ body }: DataObject): Promise<OAuthResponse> => {
5+
try {
6+
const {
7+
clientId: client_id,
8+
clientSecret: client_secret,
9+
metadata: { code, redirectUri: redirect_uri, environment },
10+
} = body;
11+
12+
const baseUrl: string =
13+
environment === 'test'
14+
? 'https://staging.southparkdata.com'
15+
: 'https://api.puzzle.io';
16+
17+
const response = await axios({
18+
url: `${baseUrl}/oauth/token`,
19+
method: 'POST',
20+
data: {
21+
grant_type: 'authorization_code',
22+
code,
23+
client_id,
24+
client_secret,
25+
redirect_uri,
26+
},
27+
});
28+
29+
const {
30+
access_token: accessToken,
31+
refresh_token: refreshToken,
32+
token_type: tokenType,
33+
expires_in: expiresIn,
34+
} = response.data;
35+
36+
return {
37+
accessToken,
38+
refreshToken,
39+
expiresIn,
40+
tokenType: tokenType === 'bearer' ? 'Bearer' : tokenType,
41+
meta: {
42+
environment,
43+
PUZZLE_BASE_URL: baseUrl,
44+
},
45+
};
46+
} catch (error) {
47+
throw new Error(`Error fetching access token for Puzzle.io: ${error}`);
48+
}
49+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import axios from 'axios';
2+
import { DataObject, OAuthResponse } from '../../lib/types';
3+
4+
export const refresh = async ({ body }: DataObject): Promise<OAuthResponse> => {
5+
try {
6+
const {
7+
OAUTH_CLIENT_ID: client_id,
8+
OAUTH_CLIENT_SECRET: client_secret,
9+
OAUTH_REFRESH_TOKEN: refresh_token,
10+
OAUTH_REQUEST_PAYLOAD: { redirectUri: redirect_uri },
11+
OAUTH_METADATA: { meta },
12+
} = body;
13+
14+
const { environment } = meta;
15+
16+
const baseUrl: string =
17+
environment === 'test'
18+
? 'https://staging.southparkdata.com'
19+
: 'https://api.puzzle.io';
20+
21+
const response = await axios({
22+
url: `${baseUrl}/oauth/token`,
23+
method: 'POST',
24+
data: {
25+
grant_type: 'refresh_token',
26+
refresh_token,
27+
client_id,
28+
client_secret,
29+
redirect_uri,
30+
},
31+
});
32+
33+
const {
34+
access_token: accessToken,
35+
token_type: tokenType,
36+
expires_in: expiresIn,
37+
} = response.data;
38+
39+
return {
40+
accessToken,
41+
refreshToken: refresh_token,
42+
expiresIn,
43+
tokenType: tokenType === 'bearer' ? 'Bearer' : tokenType,
44+
meta,
45+
};
46+
} catch (error) {
47+
throw new Error(`Error fetching access token for Puzzle.io: ${error}`);
48+
}
49+
};

0 commit comments

Comments
 (0)