Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
160484842d | ||
|
|
e3ca82ba37 | ||
|
|
9b19ca4e7c | ||
|
|
19775348ba |
6
packages/backend/src/apps/strava/assets/favicon.svg
Normal file
6
packages/backend/src/apps/strava/assets/favicon.svg
Normal file
@ -0,0 +1,6 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg"
|
||||
aria-label="Strava" role="img"
|
||||
viewBox="0 0 512 512"><rect
|
||||
width="512" height="512"
|
||||
rx="15%"
|
||||
fill="#fc4c01"/><path fill="#fff" d="M120 288L232 56l112 232h-72l-40-96-40 96z"/><path fill="#fda580" d="M280 288l32 72 32-72h48l-80 168-80-168z"/></svg>
|
||||
|
After Width: | Height: | Size: 286 B |
26
packages/backend/src/apps/strava/auth/create-auth-data.ts
Normal file
26
packages/backend/src/apps/strava/auth/create-auth-data.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { IField, IGlobalVariable } from '@automatisch/types';
|
||||
import qs from 'qs';
|
||||
|
||||
export default async function createAuthData($: IGlobalVariable) {
|
||||
try {
|
||||
const oauthRedirectUrlField = $.app.auth.fields.find(
|
||||
(field: IField) => field.key == 'oAuthRedirectUrl'
|
||||
);
|
||||
const redirectUri = oauthRedirectUrlField.value;
|
||||
const searchParams = qs.stringify({
|
||||
client_id: $.auth.data.consumerKey as string,
|
||||
redirect_uri: redirectUri,
|
||||
approval_prompt: 'force',
|
||||
response_type: 'code',
|
||||
scope: 'read_all,profile:read_all,activity:read_all,activity:write',
|
||||
});
|
||||
|
||||
await $.auth.set({
|
||||
url: `${$.app.baseUrl}/oauth/authorize?${searchParams}`,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Error occured while verifying credentials: ${error}`
|
||||
);
|
||||
}
|
||||
}
|
||||
127
packages/backend/src/apps/strava/auth/index.ts
Normal file
127
packages/backend/src/apps/strava/auth/index.ts
Normal file
@ -0,0 +1,127 @@
|
||||
import createAuthData from './create-auth-data';
|
||||
import verifyCredentials from './verify-credentials';
|
||||
import isStillVerified from './is-still-verified';
|
||||
|
||||
export default {
|
||||
fields: [
|
||||
{
|
||||
key: 'oAuthRedirectUrl',
|
||||
label: 'OAuth Redirect URL',
|
||||
type: 'string' as const,
|
||||
required: true,
|
||||
readOnly: true,
|
||||
value: '{WEB_APP_URL}/app/strava/connections/add',
|
||||
placeholder: null,
|
||||
description:
|
||||
'When asked to input an OAuth callback or redirect URL in Strava OAuth, enter the URL above.',
|
||||
clickToCopy: true,
|
||||
},
|
||||
{
|
||||
key: 'consumerKey',
|
||||
label: 'Consumer Key',
|
||||
type: 'string' as const,
|
||||
required: true,
|
||||
readOnly: false,
|
||||
value: null,
|
||||
placeholder: null,
|
||||
description: null,
|
||||
clickToCopy: false,
|
||||
},
|
||||
{
|
||||
key: 'consumerSecret',
|
||||
label: 'Consumer Secret',
|
||||
type: 'string' as const,
|
||||
required: true,
|
||||
readOnly: false,
|
||||
value: null,
|
||||
placeholder: null,
|
||||
description: null,
|
||||
clickToCopy: false,
|
||||
},
|
||||
],
|
||||
authenticationSteps: [
|
||||
{
|
||||
step: 1,
|
||||
type: 'mutation' as const,
|
||||
name: 'createConnection',
|
||||
arguments: [
|
||||
{
|
||||
name: 'key',
|
||||
value: '{key}',
|
||||
},
|
||||
{
|
||||
name: 'formattedData',
|
||||
value: null,
|
||||
properties: [
|
||||
{
|
||||
name: 'consumerKey',
|
||||
value: '{fields.consumerKey}',
|
||||
},
|
||||
{
|
||||
name: 'consumerSecret',
|
||||
value: '{fields.consumerSecret}',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
step: 2,
|
||||
type: 'mutation' as const,
|
||||
name: 'createAuthData',
|
||||
arguments: [
|
||||
{
|
||||
name: 'id',
|
||||
value: '{createConnection.id}',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
step: 3,
|
||||
type: 'openWithPopup' as const,
|
||||
name: 'openAuthPopup',
|
||||
arguments: [
|
||||
{
|
||||
name: 'url',
|
||||
value: '{createAuthData.url}',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
step: 4,
|
||||
type: 'mutation' as const,
|
||||
name: 'updateConnection',
|
||||
arguments: [
|
||||
{
|
||||
name: 'id',
|
||||
value: '{createConnection.id}',
|
||||
},
|
||||
{
|
||||
name: 'formattedData',
|
||||
value: null,
|
||||
properties: [
|
||||
{
|
||||
name: 'code',
|
||||
value: '{openAuthPopup.code}',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
step: 5,
|
||||
type: 'mutation' as const,
|
||||
name: 'verifyConnection',
|
||||
arguments: [
|
||||
{
|
||||
name: 'id',
|
||||
value: '{createConnection.id}',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
|
||||
createAuthData,
|
||||
verifyCredentials,
|
||||
isStillVerified,
|
||||
};
|
||||
13
packages/backend/src/apps/strava/auth/is-still-verified.ts
Normal file
13
packages/backend/src/apps/strava/auth/is-still-verified.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { IGlobalVariable } from '@automatisch/types';
|
||||
import getCurrentUser from '../common/get-current-user';
|
||||
|
||||
const isStillVerified = async ($: IGlobalVariable) => {
|
||||
try {
|
||||
const user = await getCurrentUser($);
|
||||
return !!user;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export default isStillVerified;
|
||||
28
packages/backend/src/apps/strava/auth/verify-credentials.ts
Normal file
28
packages/backend/src/apps/strava/auth/verify-credentials.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import qs from 'qs';
|
||||
import { IGlobalVariable } from '@automatisch/types';
|
||||
|
||||
const verifyCredentials = async ($: IGlobalVariable) => {
|
||||
try {
|
||||
const searchParams = {
|
||||
client_id: $.auth.data.consumerKey,
|
||||
client_secret: $.auth.data.consumerSecret,
|
||||
code: $.auth.data.code,
|
||||
grant_type: 'authorization_code',
|
||||
};
|
||||
const { data } = await $.http.post(
|
||||
`/v3/oauth/token?${qs.stringify(searchParams)}a`,
|
||||
);
|
||||
|
||||
await $.auth.set({
|
||||
accessToken: data.access_token,
|
||||
refreshToken: data.refresh_token,
|
||||
tokenType: data.token_type,
|
||||
userId: data.athlete.id,
|
||||
screenName: `${data.athlete.firstname} ${data.athlete.lastname}`,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error('Error occured while verifying credentials!');
|
||||
}
|
||||
};
|
||||
|
||||
export default verifyCredentials;
|
||||
13
packages/backend/src/apps/strava/common/add-auth-header.ts
Normal file
13
packages/backend/src/apps/strava/common/add-auth-header.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { TBeforeRequest } from '@automatisch/types';
|
||||
|
||||
const addAuthHeader: TBeforeRequest = ($, requestConfig) => {
|
||||
const { accessToken, tokenType } = $.auth.data;
|
||||
|
||||
if (accessToken && tokenType) {
|
||||
requestConfig.headers.Authorization = `${tokenType} ${accessToken}`;
|
||||
}
|
||||
|
||||
return requestConfig;
|
||||
};
|
||||
|
||||
export default addAuthHeader;
|
||||
10
packages/backend/src/apps/strava/common/get-current-user.ts
Normal file
10
packages/backend/src/apps/strava/common/get-current-user.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
|
||||
|
||||
const getCurrentUser = async ($: IGlobalVariable): Promise<IJSONObject> => {
|
||||
const response = await $.http.get('/v3/athlete');
|
||||
const currentUser = response.data;
|
||||
|
||||
return currentUser;
|
||||
};
|
||||
|
||||
export default getCurrentUser;
|
||||
0
packages/backend/src/apps/strava/index.d.ts
vendored
Normal file
0
packages/backend/src/apps/strava/index.d.ts
vendored
Normal file
16
packages/backend/src/apps/strava/index.ts
Normal file
16
packages/backend/src/apps/strava/index.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import defineApp from '../../helpers/define-app';
|
||||
import addAuthHeader from './common/add-auth-header';
|
||||
import auth from './auth';
|
||||
|
||||
export default defineApp({
|
||||
name: 'Strava',
|
||||
key: 'strava',
|
||||
iconUrl: '{BASE_URL}/apps/strava/assets/favicon.svg',
|
||||
authDocUrl: 'https://automatisch.io/docs/connections/strava',
|
||||
supportsConnections: true,
|
||||
baseUrl: 'https://www.strava.com',
|
||||
apiBaseUrl: 'https://www.strava.com/api',
|
||||
primaryColor: 'fc4c01',
|
||||
beforeRequest: [addAuthHeader],
|
||||
auth,
|
||||
});
|
||||
Reference in New Issue
Block a user