Skip to content

Commit

Permalink
feat: validate GitHub token
Browse files Browse the repository at this point in the history
  • Loading branch information
5n7-sk committed Jan 1, 2021
1 parent 6c410ad commit df8093a
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions cli/cli_token.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
package cli

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/google/go-github/v33/github"
"github.com/skmatz/vin"
"golang.org/x/oauth2"

"github.com/AlecAivazis/survey/v2"
)

func validateToken(token string) bool {
ctx := context.Background()
sts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
gh := github.NewClient(oauth2.NewClient(ctx, sts))

_, _, err := gh.Octocat(context.Background(), "") // random API request
return err == nil
}

const tokenGenerateURL = "https://github.com/settings/tokens/new?description=Vin" //nolint:gosec

// AskGitHubAccessToken prompts for the GitHub access token.
Expand All @@ -36,12 +50,14 @@ func (c *CLI) AskGitHubAccessToken() (string, error) {
}

fmt.Println(tokenGenerateURL)
prompt := &survey.Input{
Message: "Input your token:",
Default: token,
}
if err := survey.AskOne(prompt, &token); err != nil {
return "", err
for !validateToken(token) {
prompt := &survey.Input{
Message: "Input your token:",
Default: token,
}
if err := survey.AskOne(prompt, &token); err != nil {
return "", err
}
}
return token, nil
}
Expand Down

0 comments on commit df8093a

Please sign in to comment.