Compare commits

...
This repository has been archived on 2023-01-26. You can view files and clone it, but cannot push or open issues or pull requests.

4 Commits
main ... strava

Author SHA1 Message Date
Ali BARIN
160484842d
feat(strava): add connection verified check 2022-11-06 17:21:18 +01:00
Ali BARIN
e3ca82ba37
feat(strava): add current user utility function 2022-11-06 17:21:18 +01:00
Ali BARIN
9b19ca4e7c
feat(strava): add authentication support 2022-11-06 17:21:17 +01:00
Ali BARIN
19775348ba
feat(strava): add app definition 2022-11-06 17:21:17 +01:00
9 changed files with 239 additions and 0 deletions

View 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

View 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}`
);
}
}

View 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,
};

View 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;

View 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;

View 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;

View 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;

View File

View 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,
});