Skip to content

feat: support signing reqs with jwts in node #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .bilirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const config: Config = {
globals: {
'node-fetch': 'fetch',
},
// This is an old package that refuses to be packaged as umd.
externals: ['atlassian-jwt'],
}

const PLUGIN = process.env.PLUGIN
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"test": "jest"
},
"dependencies": {
"atlassian-jwt": "^2.0.2",
"before-after-hook": "^2.1.0",
"deepmerge": "^4.2.2",
"is-plain-object": "^3.0.0",
Expand Down
11 changes: 10 additions & 1 deletion src/plugins/auth/before-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import btoaLite from 'utils/btoa-lite'
import { createJwt } from 'utils/create-jwt'

type AuthPluginState = import('./types').AuthPluginState
type RequestOptions = import('./types').RequestOptions
Expand All @@ -9,9 +10,17 @@ export function beforeRequest(
): void {
if ('token' in state.auth) {
requestOptions.headers.authorization = `Bearer ${state.auth.token}`
} else if (state.auth.username) {
} else if ('username' in state.auth) {
const hash = btoaLite(`${state.auth.username}:${state.auth.password}`)

requestOptions.headers.authorization = `Basic ${hash}`
} else if (state.auth.appClientKey) {
const jwtValue = createJwt(
requestOptions.method,
requestOptions.url,
state.auth
)

requestOptions.headers.authorization = `JWT ${jwtValue}`
}
}
8 changes: 7 additions & 1 deletion src/plugins/auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ export type AuthToken = {
token: string
}

export type AuthOptions = AuthBasic | AuthToken
export type AuthJwt = {
appKey: string
appClientKey: string
appSharedSecret: string
}

export type AuthOptions = AuthBasic | AuthToken | AuthJwt

export type AuthPluginState = {
client: APIClient
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/auth/validate-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ type AuthOptions = import('./types').AuthOptions
export function validateOptions(auth: AuthOptions): void {
if ('token' in auth) return

if (auth.username && auth.password) return
if ('username' in auth && 'password' in auth) return

if (auth.appKey && auth.appClientKey && auth.appSharedSecret) return

throw new Error(`Invalid "auth" option: ${JSON.stringify(auth)}`)
}
14 changes: 13 additions & 1 deletion src/plugins/authenticate/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,21 @@ export function authenticate(
}
break

case 'jwt':
if (
!options.appKey ||
!options.appClientKey ||
!options.appSharedSecret
) {
throw new Error(
'JWT authentication requires an appKey, appClientKey, and appSharedSecret to be set'
)
}
break

default:
throw new Error(
"Invalid authentication type, must be 'apppassword', 'basic' or 'token'"
"Invalid authentication type, must be 'apppassword', 'basic', 'token', or 'jwt'"
)
}

Expand Down
8 changes: 8 additions & 0 deletions src/plugins/authenticate/before-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import btoaLite from 'utils/btoa-lite'
import { createJwt } from 'utils/create-jwt'

type AuthenticatePluginState = import('./types').AuthenticatePluginState
type RequestOptions = import('./types').RequestOptions
Expand All @@ -21,5 +22,12 @@ export function beforeRequest(
case 'token':
requestOptions.headers.authorization = `Bearer ${state.auth.token}`
break
case 'jwt':
requestOptions.headers.authorization = `JWT ${createJwt(
requestOptions.method,
requestOptions.url,
state.auth
)}`
break
}
}
2 changes: 2 additions & 0 deletions src/plugins/authenticate/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
type AuthBasic = import('../auth/types').AuthBasic
type AuthToken = import('../auth/types').AuthToken
type AuthJwt = import('../auth/types').AuthJwt
export type APIClient = import('../../client/types').APIClient
export type Options = import('../../client/types').Options
export type RequestOptions = import('../../endpoint/types').RequestOptions

export type AuthenticateOptions =
| (AuthBasic & { type: 'apppassword' | 'basic' })
| (AuthToken & { type: 'token' })
| (AuthJwt & { type: 'jwt' })

export type AuthenticatePluginState = {
client: APIClient
Expand Down
32 changes: 32 additions & 0 deletions src/utils/create-jwt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
fromMethodAndUrl,
createQueryStringHash,
encodeSymmetric,
SymmetricAlgorithm,
} from 'atlassian-jwt'

export function createJwt(
method: string,
fullPath: string,
auth: { appKey: string; appClientKey: string; appSharedSecret: string }
): string {
const now = Math.floor(Date.now() / 1000)
const req = fromMethodAndUrl(method, fullPath)

const tokenData = {
iss: auth.appKey,
sub: auth.appClientKey,
iat: now,
exp: now + 300,
qsh: createQueryStringHash(req),
}

// Source: https://bitbucket.org/atlassian/atlassian-connect-express/src/master/lib/middleware/authentication.js
const token = encodeSymmetric(
tokenData,
auth.appSharedSecret,
SymmetricAlgorithm.HS256
)

return token
}