Add hooks for oauth-authentication

This commit is contained in:
Jan Oberhauser 2020-09-10 10:16:24 +02:00
parent b7d46262da
commit 013a7b8cf9

View file

@ -1009,7 +1009,7 @@ class App {
const signatureMethod = _.get(oauthCredentials, 'signatureMethod') as string; const signatureMethod = _.get(oauthCredentials, 'signatureMethod') as string;
const oauth = new clientOAuth1({ const oAuthOptions: clientOAuth1.Options = {
consumer: { consumer: {
key: _.get(oauthCredentials, 'consumerKey') as string, key: _.get(oauthCredentials, 'consumerKey') as string,
secret: _.get(oauthCredentials, 'consumerSecret') as string, secret: _.get(oauthCredentials, 'consumerSecret') as string,
@ -1021,16 +1021,20 @@ class App {
.update(base) .update(base)
.digest('base64'); .digest('base64');
}, },
}); };
const callback = `${WebhookHelpers.getWebhookBaseUrl()}${this.restEndpoint}/oauth1-credential/callback?cid=${req.query.id}`; const oauthRequestData = {
oauth_callback: `${WebhookHelpers.getWebhookBaseUrl()}${this.restEndpoint}/oauth1-credential/callback?cid=${req.query.id}`
};
await this.externalHooks.run('oauth1.authenticate', [oAuthOptions, oauthRequestData]);
const oauth = new clientOAuth1(oAuthOptions);
const options: RequestOptions = { const options: RequestOptions = {
method: 'POST', method: 'POST',
url: (_.get(oauthCredentials, 'requestTokenUrl') as string), url: (_.get(oauthCredentials, 'requestTokenUrl') as string),
data: { data: oauthRequestData,
oauth_callback: callback,
},
}; };
const data = oauth.toHeader(oauth.authorize(options as RequestOptions)); const data = oauth.toHeader(oauth.authorize(options as RequestOptions));
@ -1173,7 +1177,7 @@ class App {
}; };
const stateEncodedStr = Buffer.from(JSON.stringify(state)).toString('base64') as string; const stateEncodedStr = Buffer.from(JSON.stringify(state)).toString('base64') as string;
const oAuthObj = new clientOAuth2({ const oAuthOptions: clientOAuth2.Options = {
clientId: _.get(oauthCredentials, 'clientId') as string, clientId: _.get(oauthCredentials, 'clientId') as string,
clientSecret: _.get(oauthCredentials, 'clientSecret', '') as string, clientSecret: _.get(oauthCredentials, 'clientSecret', '') as string,
accessTokenUri: _.get(oauthCredentials, 'accessTokenUrl', '') as string, accessTokenUri: _.get(oauthCredentials, 'accessTokenUrl', '') as string,
@ -1181,7 +1185,11 @@ class App {
redirectUri: `${WebhookHelpers.getWebhookBaseUrl()}${this.restEndpoint}/oauth2-credential/callback`, redirectUri: `${WebhookHelpers.getWebhookBaseUrl()}${this.restEndpoint}/oauth2-credential/callback`,
scopes: _.split(_.get(oauthCredentials, 'scope', 'openid,') as string, ','), scopes: _.split(_.get(oauthCredentials, 'scope', 'openid,') as string, ','),
state: stateEncodedStr, state: stateEncodedStr,
}); };
await this.externalHooks.run('oauth2.authenticate', [oAuthOptions]);
const oAuthObj = new clientOAuth2(oAuthOptions);
// Encrypt the data // Encrypt the data
const credentials = new Credentials(result.name, result.type, result.nodesAccess); const credentials = new Credentials(result.name, result.type, result.nodesAccess);