Skip to content
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

Adding AccessTokenMiddleware to support oauth #249

Open
wants to merge 3 commits 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
4 changes: 2 additions & 2 deletions src/SchemaRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ export default class SchemaRegistry {
public cache: Cache

constructor(
{ auth, clientId, host, retry, agent }: SchemaRegistryAPIClientArgs,
{ auth, accessTokenParams, clientId, host, retry, agent }: SchemaRegistryAPIClientArgs,
options?: SchemaRegistryAPIClientOptions,
) {
this.api = API({ auth, clientId, host, retry, agent })
this.api = API({ auth, accessTokenParams, clientId, host, retry, agent })
this.cache = new Cache()
this.options = options
}
Expand Down
6 changes: 5 additions & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Agent } from 'http'
import forge, { Authorization, Client, Options, GatewayConfiguration } from 'mappersmith'
import RetryMiddleware, { RetryMiddlewareOptions } from 'mappersmith/middleware/retry/v2'
import BasicAuthMiddleware from 'mappersmith/middleware/basic-auth'

import { DEFAULT_API_CLIENT_ID } from '../constants'
import errorMiddleware from './middleware/errorMiddleware'
import confluentEncoder from './middleware/confluentEncoderMiddleware'
import userAgentMiddleware from './middleware/userAgent'
import AccessTokenMiddleware, { AccessTokenParams } from './middleware//AccessTokenMiddleware'
import BasicAuthMiddleware from 'mappersmith/middleware/basic-auth'

const DEFAULT_RETRY = {
maxRetryTimeInSecs: 5,
Expand All @@ -19,6 +20,7 @@ const DEFAULT_RETRY = {
export interface SchemaRegistryAPIClientArgs {
host: string
auth?: Authorization
accessTokenParams?: AccessTokenParams
clientId?: string
retry?: Partial<RetryMiddlewareOptions>
/** HTTP Agent that will be passed to underlying API calls */
Expand All @@ -44,6 +46,7 @@ export type SchemaRegistryAPIClient = Client<{

export default ({
auth,
accessTokenParams,
clientId: userClientId,
host,
retry = {},
Expand All @@ -61,6 +64,7 @@ export default ({
RetryMiddleware(Object.assign(DEFAULT_RETRY, retry)),
errorMiddleware,
...(auth ? [BasicAuthMiddleware(auth)] : []),
...(accessTokenParams ? [AccessTokenMiddleware(accessTokenParams)] : []),
],
resources: {
Schema: {
Expand Down
46 changes: 46 additions & 0 deletions src/api/middleware/AccessTokenMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Middleware } from 'mappersmith'
let accessToken: string | null = null

export interface AccessTokenParams {
readonly clientCredentials: {
readonly clientId: string
readonly clientSecret: string
}
readonly authHost: string
refreshToken(authHost: string, clientId: string, clientSecret: string): Promise<string>
}

export default (params: AccessTokenParams): Middleware =>
function AccessTokenMiddleware() {
return {
async request(request) {
return Promise.resolve(accessToken)
.then(async token => {
if (token) {
return token
}
return await params.refreshToken(
params.authHost,
params.clientCredentials.clientId,
params.clientCredentials.clientSecret,
)
})
.then(token => {
accessToken = token
return request.enhance({
headers: { Authorization: token },
})
})
},
async response(next, renew) {
return next().catch(response => {
// token expired
if (response.status === 401) {
accessToken = null
return renew()
}
return next()
})
},
}
}