-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhosting-providers.ts
More file actions
230 lines (203 loc) · 7.24 KB
/
Copy pathhosting-providers.ts
File metadata and controls
230 lines (203 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import axios from 'axios';
export const TADDY_HOSTING_PROVIDER_UUID = 'e9957105-80e4-46e3-8e82-20472b9d7512';
export interface OAuthTokens {
accessToken?: string;
refreshToken?: string;
}
export interface ContentTokenResponse {
contentToken: string;
seriesUuid: string;
expiresIn: number;
}
export interface OAuthEndpoints {
signupUrl: string;
authorizeUrl: string;
tokenUrl: string;
newAccessTokenUrl: string;
newRefreshTokenUrl: string;
newContentTokenUrl: string;
publicKey: string;
instructionsUrl?: string;
claimInitiateUrl?: string;
claimCreatorUrl?: string;
}
export interface ProviderDetails {
displayName: string;
endpoints: OAuthEndpoints;
}
export const providerDetails: Record<string, ProviderDetails> = {
[TADDY_HOSTING_PROVIDER_UUID]: {
displayName: 'Taddy',
endpoints: {
signupUrl: 'https://taddy.org/developers/signup',
authorizeUrl: 'https://taddy.org/fans/authorize',
tokenUrl: 'https://taddy.org/auth/oauth2/token',
newAccessTokenUrl: 'https://taddy.org/auth/oauth2/new_access_token',
newRefreshTokenUrl: 'https://taddy.org/auth/oauth2/new_refresh_token',
newContentTokenUrl: 'https://taddy.org/auth/oauth2/new_content_token',
instructionsUrl: 'https://taddy.org/developers/instructions',
publicKey: "-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAtnihy/rnKGH9jdqWVgot\noNqWcqY2ATJE5bHtvypEf7JVqisX7yfUKC1JY1uzlLVDoMFTJdzTnAUl4xf6EpTZ\n92RGIzWDdcEk4syPSdWms855CMArTcw9fY56/egG3kYMZlVsxRysZPT5F/ovfs0H\nCFGQRsBX5vtfoFikEelInlfS0zZjljIyIZMKxPrfV/PDw+bJUJCxut3GQVf4/UnO\n4uXBPh13WyaxvMNKf1qk4CbCW1e8n17Ec3tyz4/OqVrFmtzSO9WzIEijvrQIuJQu\nNzBsgXDhPH5FZ0giYqB+ImoeURd8TirXgncv5cxcsX4sVsTXN7VBMpczmpsKMlay\nRnKo2DrBYLHPLXlKmcRq6qNNJBSXYrtk4sxRg4pFz//D0TREWM4o2T1DLgKTWqFs\nsWzs6kWVfy8KQc0ID/k2s3iK4JbxjNj3wXzkXBJTqQEahjIGesxgZqN0OlCAbXvM\ncLCeymnlRxNm4I6fZPXs7QVXmG9aGdpkWK/xNiS10WXdJxcveoud4/QH9Trq2aKl\n3bb/g71FJFfsGheoYcd+8iS9aP5lu7f91LXC5QjfKIU7JSlY4czWA1Ji9DS4Rci5\nZTMluktVO50vhr3PQxDgrB8foxxfUq4fG/ru6jBBZPk4RfNyJv6tUjkcZF0evhnm\nBqlqHmg1hzvvuyC4WW5H7LcCAwEAAQ==\n-----END PUBLIC KEY-----",
claimInitiateUrl: 'https://taddy.org/auth/claim/initiate',
claimCreatorUrl: 'https://taddy.org/dashboard/claim-creator?creator_uuid=',
}
}
}
type GetAuthorizationCodeUrlParams = {
hostingProviderUuid: string;
clientId: string;
clientUserId: string;
responseType?: string;
accessToken?: string;
seriesUuid?: string;
state?: string;
codeChallenge?: string;
codeChallengeMethod?: string;
}
/**
* Get authorization code for hosting provider
*/
export function getAuthorizationCodeUrl({
hostingProviderUuid,
clientId,
clientUserId,
responseType = 'code',
accessToken,
seriesUuid,
state,
codeChallenge,
codeChallengeMethod,
}: GetAuthorizationCodeUrlParams): string {
const authorizeUrl = providerDetails[hostingProviderUuid]?.endpoints.authorizeUrl;
if (!authorizeUrl) {
throw new Error(`Authorize URL not found for hosting provider ${hostingProviderUuid}`);
}
const url = new URL(authorizeUrl);
url.searchParams.set('client_id', clientId);
url.searchParams.set('client_user_id', clientUserId);
url.searchParams.set('response_type', responseType);
if (accessToken) {
url.searchParams.set('access_token', accessToken);
}
if (seriesUuid) {
url.searchParams.set('series_uuid', seriesUuid);
}
if (state) {
url.searchParams.set('state', state);
}
if (codeChallenge) {
url.searchParams.set('code_challenge', codeChallenge);
}
if (codeChallengeMethod) {
url.searchParams.set('code_challenge_method', codeChallengeMethod);
}
return url.toString();
}
type GetNewAccessTokenParams = {
hostingProviderUuid: string;
refreshToken: string;
}
/**
* Refresh access token using refresh token with axios
*/
export async function getNewAccessToken({
hostingProviderUuid,
refreshToken,
}: GetNewAccessTokenParams,
): Promise<string> {
try {
const newAccessTokenUrl = providerDetails[hostingProviderUuid]?.endpoints.newAccessTokenUrl;
if (!newAccessTokenUrl) {
throw new Error(`New access token URL not found for hosting provider ${hostingProviderUuid}`);
}
const response = await axios.post(newAccessTokenUrl, new URLSearchParams({
refresh_token: refreshToken,
}).toString(), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
const data = response.data;
return data.token;
} catch (error) {
if (axios.isAxiosError(error)) {
const body = error.response?.data;
const bodyStr = typeof body === 'string' ? body : JSON.stringify(body);
throw new Error(`Failed to refresh access token: status=${error.response?.status} body=${bodyStr} message=${error.message}`);
}
throw new Error(`Failed to refresh access token: ${error}`);
}
}
type GetNewRefreshTokenParams = {
hostingProviderUuid: string;
refreshToken: string;
}
/**
* Get new refresh token using axios
*/
export async function getNewRefreshToken({
hostingProviderUuid,
refreshToken,
}: GetNewRefreshTokenParams,
): Promise<string> {
try {
const newRefreshTokenUrl = providerDetails[hostingProviderUuid]?.endpoints.newRefreshTokenUrl;
if (!newRefreshTokenUrl) {
throw new Error(`New refresh token URL not found for hosting provider ${hostingProviderUuid}`);
}
const response = await axios.post(newRefreshTokenUrl, new URLSearchParams({
refresh_token: refreshToken,
}).toString(), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
const data = response.data;
return data.token;
} catch (error) {
if (axios.isAxiosError(error)) {
const body = error.response?.data;
const bodyStr = typeof body === 'string' ? body : JSON.stringify(body);
throw new Error(`Failed to get new refresh token: status=${error.response?.status} body=${bodyStr} message=${error.message}`);
}
throw new Error(`Failed to get new refresh token: ${error}`);
}
}
type GetContentTokenParams = {
hostingProviderUuid: string;
accessToken: string;
seriesUuid: string;
}
/**
* Get content token for a specific series using axios
*/
export async function getNewContentToken({
hostingProviderUuid,
accessToken,
seriesUuid,
}: GetContentTokenParams,
): Promise<string> {
try {
const newContentTokenUrl = providerDetails[hostingProviderUuid]?.endpoints.newContentTokenUrl;
if (!newContentTokenUrl) {
throw new Error(`New content token URL not found for hosting provider ${hostingProviderUuid}`);
}
const response = await axios.post(newContentTokenUrl, new URLSearchParams({
access_token: accessToken,
series_uuid: seriesUuid,
}).toString(), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Bearer ${accessToken}`,
},
});
const data = response.data;
return data.token;
} catch (error) {
if (axios.isAxiosError(error)) {
const body = error.response?.data;
const bodyStr = typeof body === 'string' ? body : JSON.stringify(body);
throw new Error(`Failed to get content token: status=${error.response?.status} body=${bodyStr} message=${error.message}`);
}
throw new Error(`Failed to get content token: ${error}`);
}
}