Skip to content

Commit

Permalink
Merge pull request #15 from nsotgui/improved-token-error-handling
Browse files Browse the repository at this point in the history
Improved token error handling
  • Loading branch information
Ryan Westlund authored Mar 23, 2021
2 parents 4299fe1 + 445cfb0 commit 778d5d4
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"golang.org/x/oauth2"
"io/ioutil"
"log"
"net/http"
"net/url"
)
Expand All @@ -26,7 +26,6 @@ type BearerToken struct {
// This method can only be called once
//
func (c *Client) RetrieveBearerToken(authorizationCode string) (*BearerToken, error) {
log.Println("Entering RetrieveBearerToken ")
client := &http.Client{}
data := url.Values{}
//set parameters
Expand All @@ -36,7 +35,7 @@ func (c *Client) RetrieveBearerToken(authorizationCode string) (*BearerToken, er

request, err := http.NewRequest("POST", string(c.discoveryAPI.TokenEndpoint), bytes.NewBufferString(data.Encode()))
if err != nil {
log.Fatalln(err)
return nil, err
}
//set headers
request.Header.Set("accept", "application/json")
Expand All @@ -47,20 +46,21 @@ func (c *Client) RetrieveBearerToken(authorizationCode string) (*BearerToken, er
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, errors.New(string(body))
}

bearerTokenResponse, err := getBearerTokenResponse([]byte(body))
log.Println("Exiting RetrieveBearerToken")
return bearerTokenResponse, err
}

//
// Call the refresh endpoint to generate new tokens
//
func (c *Client) RefreshToken(refreshToken string) (*BearerToken, error) {

log.Println("Entering RefreshToken ")
client := &http.Client{}
data := url.Values{}

Expand All @@ -70,7 +70,7 @@ func (c *Client) RefreshToken(refreshToken string) (*BearerToken, error) {

request, err := http.NewRequest("POST", string(c.discoveryAPI.TokenEndpoint), bytes.NewBufferString(data.Encode()))
if err != nil {
log.Fatalln(err)
return nil, err
}
//set the headers
request.Header.Set("accept", "application/json")
Expand All @@ -81,19 +81,22 @@ func (c *Client) RefreshToken(refreshToken string) (*BearerToken, error) {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, errors.New(string(body))
}

bearerTokenResponse, err := getBearerTokenResponse([]byte(body))
log.Println("Exiting RefreshToken ")
c.Client = getHttpClient(bearerTokenResponse)
return bearerTokenResponse, err
}

//
// Call the revoke endpoint to revoke tokens
//
func (c *Client) RevokeToken(refreshToken string) {
log.Println("Entering RevokeToken ")
func (c *Client) RevokeToken(refreshToken string) error {
client := &http.Client{}
data := url.Values{}

Expand All @@ -103,7 +106,7 @@ func (c *Client) RevokeToken(refreshToken string) {
revokeEndpoint := c.discoveryAPI.RevocationEndpoint
request, err := http.NewRequest("POST", revokeEndpoint, bytes.NewBufferString(data.Encode()))
if err != nil {
log.Fatalln(err)
return err
}
//set headers
request.Header.Set("accept", "application/json")
Expand All @@ -112,19 +115,24 @@ func (c *Client) RevokeToken(refreshToken string) {

resp, err := client.Do(request)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

if resp.StatusCode != http.StatusOK {
return errors.New(string(body))
}

responseString := map[string]string{"response": "Revoke successful"}
responseData, _ := json.Marshal(responseString)
log.Println(responseData)
log.Println("Exiting RevokeToken ")
c.Client = nil
return nil
}

func getBearerTokenResponse(body []byte) (*BearerToken, error) {
var s = new(BearerToken)
err := json.Unmarshal(body, &s)
if err != nil {
log.Fatalln("error getting BearerTokenResponse:", err)
return nil, errors.New(string(body))
}
return s, err
}
Expand Down

0 comments on commit 778d5d4

Please sign in to comment.