Skip to content

Commit

Permalink
Improve error handling and ordering of JWT code
Browse files Browse the repository at this point in the history
The if statements have been changed in order, to check for
an error first, then fall back to normal functionality. This is
to make the code more Go-idomatic, and easier to read.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Aug 24, 2020
1 parent 7af428f commit b453a73
Showing 1 changed file with 49 additions and 40 deletions.
89 changes: 49 additions & 40 deletions auth/jwt_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,55 @@ import (
jwt "github.com/dgrijalva/jwt-go"
)

// JWTAuth token issued by Github in response to signed JWT Token
type JWTAuth struct {
Token string `json:"token"`
ExpiresAt time.Time `json:"expires_at"`
}

// MakeAccessTokenForInstallation makes an access token for an installation / private key
func MakeAccessTokenForInstallation(appID string, installation int, privateKey string) (string, error) {
signed, err := GetSignedJwtToken(appID, privateKey)

if err != nil {
msg := fmt.Sprintf("can't run GetSignedJwtToken for app_id: %s and installation_id: %d, error: %v", appID, installation, err)

fmt.Printf("Error %s\n", msg)
return "", err
}

req, err := http.NewRequest(http.MethodPost,
fmt.Sprintf("https://api.github.com/app/installations/%d/access_tokens", installation), nil)
if err != nil {
return "", err
}

req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", signed))
req.Header.Add("Accept", "application/vnd.github.machine-man-preview+json")

res, err := http.DefaultClient.Do(req)

if err != nil {
msg := fmt.Sprintf("can't get access_token for app_id: %s and installation_id: %d error: %v", appID, installation, err)
fmt.Printf("Error: %s\n", msg)
return "", fmt.Errorf("%s", msg)
}

defer res.Body.Close()

bytesOut, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return "", readErr
}

jwtAuth := JWTAuth{}
jsonErr := json.Unmarshal(bytesOut, &jwtAuth)
if jsonErr != nil {
return "", jsonErr
}
return jwtAuth.Token, nil
}

// GetSignedJwtToken get a tokens signed with private key
func GetSignedJwtToken(appID string, privateKey string) (string, error) {

Expand All @@ -39,43 +88,3 @@ func GetSignedJwtToken(appID string, privateKey string) (string, error) {

return string(signedVal), nil
}

// JWTAuth token issued by Github in response to signed JWT Token
type JWTAuth struct {
Token string `json:"token"`
ExpiresAt time.Time `json:"expires_at"`
}

// MakeAccessTokenForInstallation makes an access token for an installation / private key
func MakeAccessTokenForInstallation(appID string, installation int, privateKey string) (string, error) {
signed, err := GetSignedJwtToken(appID, privateKey)

if err == nil {
c := http.Client{}
req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("https://api.github.com/app/installations/%d/access_tokens", installation), nil)

req.Header.Add("Authorization", "Bearer "+signed)
req.Header.Add("Accept", "application/vnd.github.machine-man-preview+json")

res, err := c.Do(req)

if err == nil {
defer res.Body.Close()
bytesOut, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return "", readErr
}

jwtAuth := JWTAuth{}
jsonErr := json.Unmarshal(bytesOut, &jwtAuth)
if jsonErr != nil {
return "", jsonErr
}

return jwtAuth.Token, nil
}
}
fmt.Println(fmt.Sprintf("Clound not get access token, %v", err))

return "", err
}

0 comments on commit b453a73

Please sign in to comment.