Skip to content

Commit

Permalink
feat(authorization): add bearer token authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
nithinssabu committed Nov 6, 2024
1 parent 864a507 commit b3760be
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/lib/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { Logger } from '../c8/lib/C8Logger'

const getMainEnv = () =>
createEnv({
BEARER_TOKEN: {
type: 'string',
optional: true,
},
/** Custom user agent */
CAMUNDA_CUSTOM_USER_AGENT_STRING: {
type: 'string',
Expand Down Expand Up @@ -367,8 +371,9 @@ const getEnv = () => ({

// Helper type for enforcing array contents to match an object's keys
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type EnforceArrayContent<T, K extends keyof any> =
T extends Array<K> ? T : never
type EnforceArrayContent<T, K extends keyof any> = T extends Array<K>
? T
: never

// Function to create a complete keys array, enforcing completeness at compile time
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -383,6 +388,7 @@ type CamundaEnvironmentVariables = ReturnType<typeof getMainEnv> &
export type CamundaEnvironmentVariable = keyof CamundaEnvironmentVariables
export const CamundaEnvironmentVariableDictionary =
createCompleteKeysArray<CamundaEnvironmentVariable>([
'BEARER_TOKEN',
'CAMUNDA_CONSOLE_BASE_URL',
'CAMUNDA_CONSOLE_CLIENT_ID',
'CAMUNDA_CONSOLE_CLIENT_SECRET',
Expand Down
4 changes: 4 additions & 0 deletions src/lib/ConstructOAuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import debug from 'debug'

import { NullAuthProvider, OAuthProvider } from '../oauth'
import { BasicAuthProvider } from '../oauth/lib/BasicAuthProvider'
import { BearerAuthProvider } from '../oauth/lib/BearerAuthProvider'

import { CamundaPlatform8Configuration } from './Configuration'

Expand All @@ -20,6 +21,9 @@ export function constructOAuthProvider(config: CamundaPlatform8Configuration) {
if (config.CAMUNDA_AUTH_STRATEGY === 'BASIC') {
trace(`Using Basic Auth`)
return new BasicAuthProvider({ config })
} else if (config.CAMUNDA_AUTH_STRATEGY === 'BEARER') {
trace(`Using Bearer Token`)
return new BearerAuthProvider({ config })
} else {
trace(`Using OAuth`)
return new OAuthProvider({ config })
Expand Down
30 changes: 30 additions & 0 deletions src/oauth/lib/BearerAuthProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { debug } from 'debug'

import {
CamundaEnvironmentConfigurator,
CamundaPlatform8Configuration,
DeepPartial,
} from '../../lib'
import { IOAuthProvider } from '../index'

import { TokenGrantAudienceType } from './IOAuthProvider'

export class BearerAuthProvider implements IOAuthProvider {
private bearerToken: string

constructor(options?: {
config?: DeepPartial<CamundaPlatform8Configuration>
}) {
const config = CamundaEnvironmentConfigurator.mergeConfigWithEnvironment(
options?.config ?? {}
)

this.bearerToken = config.BEARER_TOKEN || ''
}

public async getToken(audienceType: TokenGrantAudienceType): Promise<string> {
debug(`Token request for ${audienceType}`)

return Promise.resolve(this.bearerToken)
}
}

0 comments on commit b3760be

Please sign in to comment.