mirror of
https://github.com/zhigang1992/probot.git
synced 2026-06-17 23:15:30 +08:00
Move jwt creation into adapter
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import * as cacheManager from 'cache-manager'
|
||||
import * as jwt from 'jsonwebtoken'
|
||||
import {Context} from '../../context'
|
||||
import {GitHubAPI} from '../../github'
|
||||
import {logger} from '../../logger'
|
||||
@@ -17,12 +18,24 @@ function isUnauthenticatedEvent (context) {
|
||||
|
||||
export class GitHubAdapter {
|
||||
public log: LoggerWithTarget
|
||||
public jwt: () => string
|
||||
public id: number
|
||||
public cert: string
|
||||
|
||||
constructor({jwt}) {
|
||||
constructor({id, cert}) {
|
||||
this.id = id
|
||||
this.cert = cert
|
||||
this.log = wrapLogger(logger, logger)
|
||||
this.jwt = jwt
|
||||
}
|
||||
|
||||
public jwt() {
|
||||
const payload = {
|
||||
exp: Math.floor(Date.now() / 1000) + 60, // JWT expiration time
|
||||
iat: Math.floor(Date.now() / 1000), // Issued at time
|
||||
iss: this.id // GitHub App ID
|
||||
}
|
||||
|
||||
// Sign with RSA SHA256
|
||||
return jwt.sign(payload, this.cert, {algorithm: 'RS256'})
|
||||
}
|
||||
|
||||
public async createContext (event) {
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
import * as jwt from 'jsonwebtoken'
|
||||
|
||||
export const createApp = (options: AppOptions) => {
|
||||
return () => {
|
||||
const payload = {
|
||||
exp: Math.floor(Date.now() / 1000) + 60, // JWT expiration time
|
||||
iat: Math.floor(Date.now() / 1000), // Issued at time
|
||||
iss: options.id // GitHub App ID
|
||||
}
|
||||
|
||||
// Sign with RSA SHA256
|
||||
return jwt.sign(payload, options.cert, {algorithm: 'RS256'})
|
||||
}
|
||||
}
|
||||
|
||||
export interface AppOptions {
|
||||
id: number
|
||||
cert: string
|
||||
}
|
||||
@@ -14,7 +14,7 @@ export function addLogging (client: GitHubAPI, logger: Logger) {
|
||||
})
|
||||
client.hook.after('request', (result, options) => {
|
||||
const {method, url, headers, ...params} = options
|
||||
const msg = `GitHub request: ${method} ${url} - ${result.headers.status}`
|
||||
const msg = `GitHub request: ${method} ${url} - ${result.meta.status}`
|
||||
logger.debug({params}, msg)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ import * as express from 'express'
|
||||
import {GitHubAdapter} from './adapters/github'
|
||||
import {Application, WebhookEvent} from './application'
|
||||
import {Context} from './context'
|
||||
import {createApp} from './github-app'
|
||||
import {logger} from './logger'
|
||||
import {resolve} from './resolver'
|
||||
import {createServer} from './server'
|
||||
@@ -25,7 +24,6 @@ export class Probot {
|
||||
|
||||
private options: Options
|
||||
private apps: Application[]
|
||||
private app: () => string
|
||||
private adapter: GitHubAdapter
|
||||
|
||||
constructor(options: Options) {
|
||||
@@ -35,12 +33,11 @@ export class Probot {
|
||||
this.logger = logger
|
||||
this.apps = []
|
||||
this.webhook = new Webhooks({path: options.webhookPath, secret: options.secret})
|
||||
this.app = createApp({ id: options.id, cert: options.cert })
|
||||
this.server = createServer({logger})
|
||||
this.server.use(this.webhook.middleware)
|
||||
|
||||
|
||||
this.adapter = new GitHubAdapter({jwt: this.app})
|
||||
this.adapter = new GitHubAdapter({ id: options.id, cert: options.cert })
|
||||
|
||||
// Log all received webhooks
|
||||
this.webhook.on('*', (event: any) => {
|
||||
|
||||
@@ -5,7 +5,8 @@ const {GitHubAdapter} = require('../../src/adapters/github')
|
||||
|
||||
module.exports = {
|
||||
createApp (plugin = () => {}) {
|
||||
const adapter = new GitHubAdapter({jwt: jest.fn().mockReturnValue('test')})
|
||||
const adapter = new GitHubAdapter({})
|
||||
adapter.jwt = jest.fn().mockReturnValue('test')
|
||||
const app = new Application({adapter})
|
||||
plugin(app)
|
||||
return app
|
||||
|
||||
Reference in New Issue
Block a user