diff --git a/src/auth.ts b/src/auth.ts index 1a3fecdac8..7fda485531 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -2,8 +2,7 @@ import {shell} from 'electron' import * as keytar from 'keytar' import guid from './lib/guid' - -const ServiceName = 'GitHubClient' +import User from './user' const ClientID = 'de0e3c7e9973e1c4dd77' const ClientSecret = '4b35aab1581a32e23af0d930f2a294ae3bb84960' @@ -21,19 +20,19 @@ interface AuthState { } let authState: AuthState = null -export function requestToken(code: string): Promise { - return fetch(`${authState.endpoint}/login/oauth/access_token`, { - method: 'post', - headers: DefaultHeaders, - body: JSON.stringify({ - 'client_id': ClientID, - 'client_secret': ClientSecret, - 'code': code, - 'state': authState - }) +export async function requestToken(code: string): Promise { + const response = await fetch(`${authState.endpoint}/login/oauth/access_token`, { + method: 'post', + headers: DefaultHeaders, + body: JSON.stringify({ + 'client_id': ClientID, + 'client_secret': ClientSecret, + 'code': code, + 'state': authState }) - .then(response => response.json()) - .then(response => response.access_token) + }) + const json = await response.json() + return json.access_token } function getOAuthURL(authState: AuthState): string { @@ -50,10 +49,16 @@ export function askUserToAuth(endpoint: string) { shell.openExternal(getOAuthURL(authState)) } -export function getToken(username: string): string { - return keytar.getPassword(ServiceName, username) +export function getToken(user: User): string { + const serviceName = getServiceNameForUser(user) + return keytar.getPassword(serviceName, user.getLogin()) } -export function setToken(username: string, token: string) { - keytar.addPassword(ServiceName, username, token) +export function setToken(user: User, token: string) { + const serviceName = getServiceNameForUser(user) + keytar.addPassword(serviceName, user.getLogin(), token) +} + +function getServiceNameForUser(user: User): string { + return `GitHub – ${user.getEndpoint()}` } diff --git a/src/index.tsx b/src/index.tsx index 8637edc980..9cca8f2d51 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -4,7 +4,7 @@ import * as ReactDOM from 'react-dom' import {ipcRenderer} from 'electron' import App from './app' -import {requestToken} from './auth' +import {requestToken, getDotComEndpoint} from './auth' import {URLActionType, OAuthAction} from './lib/parse-url' import UsersStore from './users-store' import User from './user' @@ -37,7 +37,7 @@ async function addUserWithCode(code: string) { const token = await requestToken(code) const octo = new Octokat({token}) const user = await octo.user.fetch() - usersStore.addUser(new User(user.login, token)) + usersStore.addUser(new User(user.login, getDotComEndpoint(), token)) } catch (e) { console.error(`Error adding user: ${e}`) } diff --git a/src/info.tsx b/src/info.tsx index 1219d6b024..e3b1e610dc 100644 --- a/src/info.tsx +++ b/src/info.tsx @@ -50,7 +50,7 @@ export default class Info extends React.Component { } public async componentWillMount() { - const api = new Octokat({token: this.props.user.token}) + const api = new Octokat({token: this.props.user.getToken()}) const user = await api.user.fetch() this.setState({userAvatarURL: user.avatarUrl}) console.log('user', user) diff --git a/src/user.ts b/src/user.ts index a159e50472..8a8c296ecc 100644 --- a/src/user.ts +++ b/src/user.ts @@ -1,10 +1,27 @@ export default class User { - public token: string - public login: string - public endpoint: string + private token: string + private login: string + private endpoint: string - public constructor(login: string, token: string) { + public constructor(login: string, endpoint: string, token: string) { this.login = login + this.endpoint = endpoint this.token = token } + + public getToken(): string { + return this.token + } + + public getLogin(): string { + return this.login + } + + public getEndpoint(): string { + return this.endpoint + } + + public userWithToken(token: string): User { + return new User(this.login, this.endpoint, token) + } } diff --git a/src/users-store.ts b/src/users-store.ts index f85c26eef7..a1a981a379 100644 --- a/src/users-store.ts +++ b/src/users-store.ts @@ -28,7 +28,7 @@ export default class UsersStore { } public addUser(user: User) { - setToken(user.login, user.token) + setToken(user, user.getToken()) this.users.push(user) this.usersDidChange() @@ -47,14 +47,14 @@ export default class UsersStore { } const rawUsers: User[] = JSON.parse(raw) - const usersWithTokens = rawUsers.map(user => new User(user.login, getToken(user.login))) + const usersWithTokens = rawUsers.map(user => user.userWithToken(getToken(user))) this.users = usersWithTokens this.usersDidChange() } private saveToDisk() { - const usersWithoutTokens = this.users.map(user => new User(user.login, '')) + const usersWithoutTokens = this.users.map(user => user.userWithToken('')) localStorage.setItem('users', JSON.stringify(usersWithoutTokens)) } }