Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obtain a current token to validate #2

Merged
merged 1 commit into from
Jan 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions cmd/radioauth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
)

func authenticateToken(account *account.Account) bool {
oauth2Token := oauth2.Token{
storedOauth2Token := oauth2.Token{
AccessToken: account.AccessToken,
RefreshToken: account.RefreshToken,
Expiry: account.TokenExpiry,
Expand All @@ -43,15 +43,21 @@ func authenticateToken(account *account.Account) bool {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

tokenSource := oauthConfig.TokenSource(ctx, &oauth2Token)
tokenSource := oauthConfig.TokenSource(ctx, &storedOauth2Token)

userinfo, err := provider.UserInfo(ctx, tokenSource)
if err != nil {
log.Printf("[authenticate] OAuth provider rejected %s: %v", account.Username, err)
return false
}

if !oauth2Token.Valid() {
currentOauth2Token, err := tokenSource.Token()
if err != nil {
log.Printf("[authenitaction] Unable to obtain OAuth Token for user %s: %v", account.Username, err)
return false
}

if !currentOauth2Token.Valid() {
log.Printf("[authenticate] OAuth token for user %s is invalid!", account.Username)
return false
}
Expand All @@ -67,7 +73,10 @@ func authenticateToken(account *account.Account) bool {
}

// Sync back the (possibly refreshed) access token
account.AccessToken = oauth2Token.AccessToken
account.AccessToken = currentOauth2Token.AccessToken
account.RefreshToken = currentOauth2Token.RefreshToken
account.TokenExpiry = currentOauth2Token.Expiry

err = accountStore.Write(account)
if err != nil {
log.Printf("[authenticate] Cannot write back account info for %s: %v", userinfo.Email, err)
Expand Down