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

Add ability to add certs, proxy and oauth #269

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
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, oauth, ca, cert, key, proxy, clientId, host, retry, agent }: SchemaRegistryAPIClientArgs,
options?: SchemaRegistryAPIClientOptions,
) {
this.api = API({ auth, clientId, host, retry, agent })
this.api = API({ auth, oauth, ca, cert, key, proxy, clientId, host, retry, agent })
this.cache = new Cache()
this.options = options
}
Expand Down
12 changes: 12 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DEFAULT_API_CLIENT_ID } from '../constants'
import errorMiddleware from './middleware/errorMiddleware'
import confluentEncoder from './middleware/confluentEncoderMiddleware'
import userAgentMiddleware from './middleware/userAgent'
import AccessSecurityMiddleware from './middleware/AccessSecurityMiddleware'

const DEFAULT_RETRY = {
maxRetryTimeInSecs: 5,
Expand All @@ -19,6 +20,11 @@ const DEFAULT_RETRY = {
export interface SchemaRegistryAPIClientArgs {
host: string
auth?: Authorization
oauth?:string
ca?: Buffer
cert?: Buffer
key?: Buffer
proxy?:string
clientId?: string
retry?: Partial<RetryMiddlewareOptions>
/** HTTP Agent that will be passed to underlying API calls */
Expand All @@ -44,6 +50,11 @@ export type SchemaRegistryAPIClient = Client<{

export default ({
auth,
oauth,
ca,
cert,
key,
proxy,
clientId: userClientId,
host,
retry = {},
Expand All @@ -61,6 +72,7 @@ export default ({
RetryMiddleware(Object.assign(DEFAULT_RETRY, retry)),
errorMiddleware,
...(auth ? [BasicAuthMiddleware(auth)] : []),
...(oauth ? [AccessSecurityMiddleware({oauth, ca, cert, key, proxy})] : []),
],
resources: {
Schema: {
Expand Down
34 changes: 34 additions & 0 deletions src/api/middleware/AccessSecurityMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Middleware } from 'mappersmith'

export interface AccessTokenParams {
readonly oauth: string
readonly ca?: string | Buffer
readonly cert?: string | Buffer
readonly key?: string | Buffer
readonly proxy?: string
}

export default (params: AccessTokenParams): Middleware =>
function AccessSecurityMiddleware() {
return {
async request(request) {
return request.enhance({
headers: {
Authorization: `Bearer ${params.oauth}`,
},
ca: params.ca,
cert: params.cert,
key: params.key,
proxy: params.proxy,
})
},
async response(next, renew) {
return next().catch(response => {
if (response.status === 401) {
return renew()
}
return next()
})
},
}
}
Loading