Skip to content

Commit 88ae03f

Browse files
committed
OAuth1 support
1 parent cf114d4 commit 88ae03f

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@types/express": "^4.17.8",
3030
"@types/jest": "^26.0.15",
3131
"@types/node": "^14.14.5",
32+
"@types/oauth": "^0.9.1",
3233
"@types/sinon": "^9.0.8",
3334
"@typescript-eslint/eslint-plugin": "^4.6.0",
3435
"@typescript-eslint/parser": "^4.6.0",
@@ -44,6 +45,8 @@
4445
},
4546
"dependencies": {
4647
"atlassian-jwt": "^1.0.3",
47-
"axios": "^0.21.0"
48+
"axios": "^0.21.0",
49+
"oauth": "^0.9.15",
50+
"qs": "^6.9.4"
4851
}
4952
}

src/config.ts

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export namespace Config {
1717
jwt?: Authentication.JWT;
1818
accessToken?: Authentication.AccessToken;
1919
basic?: Authentication.Basic;
20+
oauth1?: Authentication.OAuth1;
2021
}
2122

2223
export interface Middlewares {
@@ -42,6 +43,13 @@ export namespace Config {
4243
password?: string;
4344
};
4445

46+
export type OAuth1 = {
47+
consumerKey: string;
48+
consumerSecret: string;
49+
accessToken: string;
50+
tokenSecret: string;
51+
};
52+
4553
export type AccessToken = string;
4654
}
4755
}

src/helpers/index.ts

+22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { AxiosRequestConfig } from 'axios';
22
import * as jwt from 'atlassian-jwt';
33
import * as url from 'url';
4+
import { OAuth } from 'oauth';
5+
import * as qs from 'qs';
46
import { Config } from '../config';
57

68
export const getAuthentication = (
@@ -30,6 +32,26 @@ export const getAuthentication = (
3032
return `JWT ${jwtToken}`;
3133
}
3234

35+
if (config.authentication?.oauth1) {
36+
const oauthUrl = `${config.host}/plugins/servlet/oauth/`;
37+
const oauth = new OAuth(
38+
`${oauthUrl}request-token`,
39+
`${oauthUrl}access-token`,
40+
config.authentication.oauth1.consumerKey,
41+
config.authentication.oauth1.consumerSecret,
42+
'1.0',
43+
null,
44+
'RSA-SHA1',
45+
);
46+
47+
return oauth.authHeader(
48+
`${config.host + request.url!}?${qs.stringify(request.params)}`,
49+
config.authentication.oauth1.accessToken,
50+
config.authentication.oauth1.tokenSecret,
51+
request.method!,
52+
);
53+
}
54+
3355
if (config.authentication?.accessToken) {
3456
return `Bearer ${config.authentication.accessToken}`;
3557
}

yarn.lock

+17
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,13 @@
667667
resolved "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz"
668668
integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA==
669669

670+
"@types/oauth@^0.9.1":
671+
version "0.9.1"
672+
resolved "https://registry.yarnpkg.com/@types/oauth/-/oauth-0.9.1.tgz#e17221e7f7936b0459ae7d006255dff61adca305"
673+
integrity sha512-a1iY62/a3yhZ7qH7cNUsxoI3U/0Fe9+RnuFrpTKr+0WVOzbKlSLojShCKe20aOD1Sppv+i8Zlq0pLDuTJnwS4A==
674+
dependencies:
675+
"@types/node" "*"
676+
670677
"@types/prettier@^2.0.0":
671678
version "2.1.1"
672679
resolved "https://registry.npmjs.org/@types/prettier/-/prettier-2.1.1.tgz"
@@ -3460,6 +3467,11 @@ oauth-sign@~0.9.0:
34603467
resolved "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz"
34613468
integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ==
34623469

3470+
oauth@^0.9.15:
3471+
version "0.9.15"
3472+
resolved "https://registry.yarnpkg.com/oauth/-/oauth-0.9.15.tgz#bd1fefaf686c96b75475aed5196412ff60cfb9c1"
3473+
integrity sha1-vR/vr2hslrdUda7VGWQS/2DPucE=
3474+
34633475
object-copy@^0.1.0:
34643476
version "0.1.0"
34653477
resolved "https://registry.npmjs.org/object-copy/-/object-copy-0.1.0.tgz"
@@ -3783,6 +3795,11 @@ punycode@^2.1.0, punycode@^2.1.1:
37833795
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz"
37843796
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==
37853797

3798+
qs@^6.9.4:
3799+
version "6.9.4"
3800+
resolved "https://registry.yarnpkg.com/qs/-/qs-6.9.4.tgz#9090b290d1f91728d3c22e54843ca44aea5ab687"
3801+
integrity sha512-A1kFqHekCTM7cz0udomYUoYNWjBebHm/5wzU/XqrBRBNWectVH0QIiN+NEcZ0Dte5hvzHwbr8+XQmguPhJ6WdQ==
3802+
37863803
qs@~6.5.2:
37873804
version "6.5.2"
37883805
resolved "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz"

0 commit comments

Comments
 (0)