feat(flickr): add authentication

This commit is contained in:
Ali BARIN
2022-10-21 01:33:40 +02:00
parent 05075fc1c4
commit 71d6a57cdc
19 changed files with 380 additions and 814 deletions

View File

@@ -0,0 +1,31 @@
import { IJSONObject, IField, IGlobalVariable } from '@automatisch/types';
import { URLSearchParams } from 'url';
export default async function createAuthData($: IGlobalVariable) {
try {
const oauthRedirectUrlField = $.app.auth.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
);
const callbackUrl = oauthRedirectUrlField.value;
const requestPath = '/oauth/request_token';
const data = { oauth_callback: callbackUrl };
const response = await $.http.post(requestPath, data);
const responseData = Object.fromEntries(new URLSearchParams(response.data));
await $.auth.set({
url: `${$.app.apiBaseUrl}/oauth/authorize?oauth_token=${responseData.oauth_token}&perms=delete`,
accessToken: responseData.oauth_token,
accessSecret: responseData.oauth_token_secret,
});
} catch (error) {
const errorMessages = error.response.data.errors
.map((error: IJSONObject) => error.message)
.join(' ');
throw new Error(
`Error occured while verifying credentials: ${errorMessages}`
);
}
}

View File

@@ -0,0 +1,221 @@
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',
required: true,
readOnly: true,
value: '{WEB_APP_URL}/app/flickr/connections/add',
placeholder: null,
description: 'When asked to input an OAuth callback or redirect URL in Flickr OAuth, enter the URL above.',
docUrl: 'https://automatisch.io/docs/flickr#oauth-redirect-url',
clickToCopy: true
},
{
key: 'consumerKey',
label: 'Consumer Key',
type: 'string',
required: true,
readOnly: false,
value: null,
placeholder: null,
description: null,
docUrl: 'https://automatisch.io/docs/flickr#consumer-key',
clickToCopy: false
},
{
key: 'consumerSecret',
label: 'Consumer Secret',
type: 'string',
required: true,
readOnly: false,
value: null,
placeholder: null,
description: null,
docUrl: 'https://automatisch.io/docs/flickr#consumer-secret',
clickToCopy: false
}
],
authenticationSteps: [
{
step: 1,
type: 'mutation',
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',
name: 'createAuthData',
arguments: [
{
name: 'id',
value: '{createConnection.id}'
}
]
},
{
step: 3,
type: 'openWithPopup',
name: 'openAuthPopup',
arguments: [
{
name: 'url',
value: '{createAuthData.url}'
}
]
},
{
step: 4,
type: 'mutation',
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}'
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'oauthVerifier',
value: '{openAuthPopup.oauth_verifier}'
}
]
}
]
},
{
step: 5,
type: 'mutation',
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}'
}
]
}
],
reconnectionSteps: [
{
step: 1,
type: 'mutation',
name: 'resetConnection',
arguments: [
{
name: 'id',
value: '{connection.id}'
}
]
},
{
step: 2,
type: 'mutation',
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{connection.id}'
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'consumerKey',
value: '{fields.consumerKey}'
},
{
name: 'consumerSecret',
value: '{fields.consumerSecret}'
}
]
}
]
},
{
step: 3,
type: 'mutation',
name: 'createAuthData',
arguments: [
{
name: 'id',
value: '{connection.id}'
}
]
},
{
step: 4,
type: 'openWithPopup',
name: 'openAuthPopup',
arguments: [
{
name: 'url',
value: '{createAuthData.url}'
}
]
},
{
step: 5,
type: 'mutation',
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{connection.id}'
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'oauthVerifier',
value: '{openAuthPopup.oauth_verifier}'
}
]
}
]
},
{
step: 6,
type: 'mutation',
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{connection.id}'
}
]
}
],
createAuthData,
verifyCredentials,
isStillVerified,
};

View File

@@ -0,0 +1,17 @@
import { IGlobalVariable } from '@automatisch/types';
const isStillVerified = async ($: IGlobalVariable) => {
try {
const params = {
method: 'flickr.test.login',
format: 'json',
nojsoncallback: 1,
}
const response = await $.http.get('/rest', { params });
return !!response.data.user.id;
} catch (error) {
return false;
}
};
export default isStillVerified;

View File

@@ -0,0 +1,26 @@
import { IGlobalVariable } from '@automatisch/types';
import { URLSearchParams } from 'url';
const verifyCredentials = async ($: IGlobalVariable) => {
try {
const response = await $.http.post(
`/oauth/access_token?oauth_verifier=${$.auth.data.oauthVerifier}&oauth_token=${$.auth.data.accessToken}`,
null
);
const responseData = Object.fromEntries(new URLSearchParams(response.data));
await $.auth.set({
consumerKey: $.auth.data.consumerKey,
consumerSecret: $.auth.data.consumerSecret,
accessToken: responseData.oauth_token,
accessSecret: responseData.oauth_token_secret,
userId: responseData.user_nsid,
screenName: responseData.fullname,
});
} catch (error) {
throw new Error(error.response.data);
}
};
export default verifyCredentials;