Skip to content

Commit

Permalink
Change the way of adding "Authorization" header to github api requests
Browse files Browse the repository at this point in the history
  • Loading branch information
vinogradov-m committed Mar 16, 2017
1 parent bbdc29a commit 3b0668d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/electron-builder-http/src/httpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export abstract class HttpExecutor<REQUEST_OPTS, REQUEST> {
reject(new HttpError(response, isJson ? JSON.parse(data) : data))
}
else {
const pathname = (<any>options).pathname || options.path
const pathname = (<any>options).originalFilename || (<any>options).pathname || options.path
if (data.length === 0) {
resolve()
}
Expand Down Expand Up @@ -309,7 +309,7 @@ export function dumpRequestOptions(options: RequestOptions): string {

// requestOptions should be cloned already, modified in place
function removeAuthHeader(requestOptions: RequestOptions): RequestOptions {
// github redirect to amazon s3 - avoid error "Only one auth mechanism allowed"
// github redirect to amazon s3 - avoid error "Only one auth mechanism allowed"
if (requestOptions.headers != null && (requestOptions.hostname || "").includes(".amazonaws.") && requestOptions.headers.Authorization != null && requestOptions.headers.Authorization.startsWith("token ")) {
requestOptions.headers = Object.assign({}, requestOptions.headers)
delete requestOptions.headers.Authorization
Expand Down
45 changes: 36 additions & 9 deletions packages/electron-updater/src/PrivateGitHubProvider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { session } from "electron"
import { HttpError, request } from "electron-builder-http"
import { CancellationToken } from "electron-builder-http/out/CancellationToken"
import { GithubOptions, UpdateInfo } from "electron-builder-http/out/publishOptions"
Expand All @@ -15,16 +16,18 @@ export interface PrivateGitHubUpdateInfo extends UpdateInfo {
export class PrivateGitHubProvider extends BaseGitHubProvider<PrivateGitHubUpdateInfo> {
constructor(options: GithubOptions, private readonly token: string) {
super(options, "api.github.com")
this.addHeadersHook()
}

async getLatestVersion(): Promise<PrivateGitHubUpdateInfo> {
const basePath = this.basePath
const cancellationToken = new CancellationToken()
const channelFile = getChannelFilename(getDefaultChannelName())

const assets = await this.getLatestVersionInfo(basePath, cancellationToken)
const requestOptions = Object.assign({
headers: this.configureHeaders("application/octet-stream")
headers: this.configureHeaders("application/octet-stream"),
originalFilename: channelFile

This comment has been minimized.

Copy link
@vinogradov-m

vinogradov-m Mar 16, 2017

Author Owner

The path to file doesn't contain filename when it's downloaded from private repo on GitHub and a content type which can be read from the response is "application/octet-stream".
So, it needs to save original filename to be able to detect if it's json or yml or something else further.

}, parseUrl(assets.find(it => it.name == channelFile)!.url))
let result: any
try {
Expand All @@ -45,13 +48,25 @@ export class PrivateGitHubProvider extends BaseGitHubProvider<PrivateGitHubUpdat
return result
}

private addHeadersHook(): void {
const { protocol, hostname } = this.baseUrl
const filter = {
urls: [`${protocol}//${hostname}${this.basePath}/*`]
}
session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details: WebRequestDetails, callback: Function) => {
if (!details.requestHeaders.hasOwnProperty("Authorization")) {
details.requestHeaders.Authorization = `token ${this.token}`
}
callback({cancel: false, requestHeaders: details.requestHeaders})
})
};

private configureHeaders(accept: string) {
return Object.assign({
Accept: accept,
Authorization: `token ${this.token}`,
Accept: accept
}, this.requestHeaders)
}

private async getLatestVersionInfo(basePath: string, cancellationToken: CancellationToken): Promise<Array<Asset>> {
const requestOptions: RequestOptions = Object.assign({
path: `${basePath}/latest`,
Expand All @@ -71,10 +86,9 @@ export class PrivateGitHubProvider extends BaseGitHubProvider<PrivateGitHubUpdat

async getUpdateFile(versionInfo: PrivateGitHubUpdateInfo): Promise<FileInfo> {
const headers = {
Accept: "application/octet-stream",
Authorization: `token ${this.token}`
Accept: "application/octet-stream"
}

// space is not supported on GitHub
if (getCurrentPlatform() === "darwin") {
const info = <any>versionInfo
Expand All @@ -99,4 +113,17 @@ export class PrivateGitHubProvider extends BaseGitHubProvider<PrivateGitHubUpdat
export interface Asset {
name: string
url: string
}

export interface HeaderList {
[key: string]: any
}

export interface WebRequestDetails {
id: number
url: string
method: string
resourceType: string
timestamp: number
requestHeaders: HeaderList
}

0 comments on commit 3b0668d

Please sign in to comment.