Skip to content

Commit 5601540

Browse files
authored
OAuth1 support (MrRefactoring#89)
1 parent 68c0546 commit 5601540

File tree

6 files changed

+62
-2
lines changed

6 files changed

+62
-2
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ const client = new Client({
111111
});
112112
```
113113

114+
### OAuth1.0 authentication
115+
116+
```js
117+
const client = new Client({
118+
host: "https://jira.somehost.com",
119+
authentication: {
120+
oauth1: {
121+
consumerKey: "your consumer key",
122+
consumerSecret: "-----BEGIN RSA PRIVATE KEY-----\n" + "some private key\n" + "-----END RSA PRIVATE KEY-----",
123+
accessToken: "your access token",
124+
tokenSecret: "your token secret"
125+
}
126+
}
127+
});
128+
```
129+
114130
### OAuth2.0 authentication
115131

116132
```js

package.json

+3-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,7 @@
4445
},
4546
"dependencies": {
4647
"atlassian-jwt": "^1.0.3",
47-
"axios": "^0.21.0"
48+
"axios": "^0.21.0",
49+
"oauth": "^0.9.15"
4850
}
4951
}

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

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AxiosRequestConfig } from 'axios';
22
import * as jwt from 'atlassian-jwt';
33
import * as url from 'url';
4+
import { OAuth } from 'oauth';
45
import { Config } from '../config';
56

67
export const getAuthentication = (
@@ -30,6 +31,26 @@ export const getAuthentication = (
3031
return `JWT ${jwtToken}`;
3132
}
3233

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

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ export class Client {
293293
public async sendRequest(request: AxiosRequestConfig, callback?: Callback): Promise<any> {
294294
try {
295295
request.headers = Utils.removeUndefinedElements({
296-
Authorization: getAuthentication(this.config, request),
296+
Authorization: getAuthentication(this.config,
297+
{ ...request, url: this.requestInstance.getUri(request) }),
297298
...request.headers,
298299
});
299300

yarn.lock

+12
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"
@@ -3467,6 +3474,11 @@ oauth-sign@~0.9.0:
34673474
resolved "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz"
34683475
integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ==
34693476

3477+
oauth@^0.9.15:
3478+
version "0.9.15"
3479+
resolved "https://registry.yarnpkg.com/oauth/-/oauth-0.9.15.tgz#bd1fefaf686c96b75475aed5196412ff60cfb9c1"
3480+
integrity sha1-vR/vr2hslrdUda7VGWQS/2DPucE=
3481+
34703482
object-copy@^0.1.0:
34713483
version "0.1.0"
34723484
resolved "https://registry.npmjs.org/object-copy/-/object-copy-0.1.0.tgz"

0 commit comments

Comments
 (0)