mirror of
https://github.com/desktop/desktop
synced 2024-10-31 11:07:25 +00:00
Scope auth by the endpoint.
This commit is contained in:
parent
369f33a591
commit
60ce7c7c9c
5 changed files with 50 additions and 28 deletions
25
src/auth.ts
25
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,8 +20,8 @@ interface AuthState {
|
|||
}
|
||||
let authState: AuthState = null
|
||||
|
||||
export function requestToken(code: string): Promise<string> {
|
||||
return fetch(`${authState.endpoint}/login/oauth/access_token`, {
|
||||
export async function requestToken(code: string): Promise<string> {
|
||||
const response = await fetch(`${authState.endpoint}/login/oauth/access_token`, {
|
||||
method: 'post',
|
||||
headers: DefaultHeaders,
|
||||
body: JSON.stringify({
|
||||
|
@ -32,8 +31,8 @@ export function requestToken(code: string): Promise<string> {
|
|||
'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()}`
|
||||
}
|
||||
|
|
|
@ -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}`)
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ export default class Info extends React.Component<InfoProps, InfoState> {
|
|||
}
|
||||
|
||||
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)
|
||||
|
|
25
src/user.ts
25
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue