Skip to content

Commit 06d9dbb

Browse files
authored
Merge pull request #38 from pulsar-edit/allow-parent-scopes
lib: Allow parent scopes when checking if each required scope is set
2 parents 65eacd2 + 6e800cb commit 06d9dbb

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

lib/models/github-login-model.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let instance = null;
88
export default class GithubLoginModel {
99
// Be sure that we're requesting at least this many scopes on the token we grant through github.atom.io or we'll
1010
// give everyone a really frustrating experience ;-)
11-
static REQUIRED_SCOPES = ['repo', 'read:org', 'user:email']
11+
static REQUIRED_SCOPES = ['public_repo', 'read:org', 'user:email']
1212

1313
static get() {
1414
if (!instance) {
@@ -70,7 +70,25 @@ export default class GithubLoginModel {
7070

7171
for (const scope of this.constructor.REQUIRED_SCOPES) {
7272
if (!scopeSet.has(scope)) {
73+
if (scope === 'public_repo' && scopeSet.has('repo')) {
74+
// 'repo' is a superset of, and implies, 'public_repo'.
75+
// Setting just 'public_repo' or full 'repo' both have legitimate use-cases. So we won't warn about it.
76+
continue;
77+
}
78+
if (scope === 'read:org' && scopeSet.has('admin:org')) {
79+
// 'admin:org' is a superset of, and implies, 'read:org'.
80+
console.warn('Excessive scopes detected on your github token. Please only set the actually needed scopes on your PAT.')
81+
console.warn('Excessive scope "admin:org" should be "read:org" instead.')
82+
continue;
83+
}
84+
if (scope === 'user:email' && scopeSet.has('user')) {
85+
// 'user' is a superset of, and implies, 'user:email'.
86+
console.warn('Excessive scopes detected on your github token. Please only set the actually needed scopes on your PAT.')
87+
console.warn('Excessive scope "user" should be "user:email" instead.')
88+
continue;
89+
}
7390
// Token doesn't have enough OAuth scopes, need to reauthenticate
91+
console.log("GitHub token doesn't have a required scope! Missing: " + scope);
7492
this.checked.set(fingerprint, INSUFFICIENT);
7593
return INSUFFICIENT;
7694
}

lib/views/github-login-view.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33

44
import {autobind} from '../helpers';
5+
import {INSUFFICIENT} from '../shared/keytar-strategy';
56

67
export default class GithubLoginView extends React.Component {
78
static propTypes = {
89
children: PropTypes.node,
910
onLogin: PropTypes.func,
11+
tokenStatus: PropTypes.symbol,
1012
}
1113

1214
static defaultProps = {
@@ -15,6 +17,7 @@ export default class GithubLoginView extends React.Component {
1517
<span>Log in to GitHub to access PR information and more!</span>
1618
</div>,
1719
onLogin: token => {},
20+
tokenStatus: Symbol(),
1821
}
1922

2023
constructor(props, context) {
@@ -57,22 +60,34 @@ export default class GithubLoginView extends React.Component {
5760
);
5861
}
5962

63+
renderTokenHint() {
64+
if (this.props.tokenStatus === INSUFFICIENT) {
65+
return(<span>Hint: Entered token has insufficient scopes. Update the scopes on your token and try again. See Dev Tools console for details.</span>);
66+
}
67+
}
68+
6069
renderTokenInput() {
70+
const tokenHint = this.renderTokenHint();
71+
6172
return (
6273
<form className="github-GithubLoginView-Subview" onSubmit={this.handleSubmitToken}>
6374
<div className="github-GitHub-LargeIcon icon icon-mark-github" />
6475
<h1>Enter Token</h1>
6576
<ol>
6677
<li>
67-
Visit <a href="https://github.com/settings/tokens">github.com/settings/tokens</a> to generate a new
68-
Personal Access Token (classic).<sup><a href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token#creating-a-personal-access-token-classic">[docs]</a></sup>
78+
Visit <a href="https://github.com/settings/tokens/new?scopes=repo,workflow,user:email,read:org&description=Pulsar%20github%20package">
79+
github.com/settings/tokens
80+
</a> to generate a new Personal Access Token (classic).
81+
<sup><a href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token#creating-a-personal-access-token-classic">[docs]</a></sup>
6982
</li>
7083
<li>
7184
Ensure it has the following permissions: <code>repo</code>, <code>workflow</code>, <code>read:org</code>, and <code>user:email</code>.
7285
</li>
7386
<li>Enter the token below:</li>
7487
</ol>
7588

89+
{tokenHint}
90+
7691
<input
7792
type="text"
7893
className="input-text native-key-bindings"

lib/views/github-tab-view.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export default class GitHubTabView extends React.Component {
8181

8282
if (this.props.token === INSUFFICIENT) {
8383
return (
84-
<GithubLoginView onLogin={this.props.handleLogin}>
84+
<GithubLoginView onLogin={this.props.handleLogin} tokenStatus={INSUFFICIENT}>
8585
<p>
8686
Your token no longer has sufficient authorizations. Please re-authenticate and generate a new one.
8787
</p>

0 commit comments

Comments
 (0)