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

Allow for RS256 Token Signing #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 28 additions & 2 deletions api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package api

import (
"context"
"crypto/rsa"
"io/ioutil"
"net/http"

jwt "github.com/dgrijalva/jwt-go"
Expand Down Expand Up @@ -36,13 +38,37 @@ func (a *API) extractBearerToken(w http.ResponseWriter, r *http.Request) (string

func (a *API) parseJWTClaims(bearer string, r *http.Request) (context.Context, error) {
config := getConfig(r.Context())
p := jwt.Parser{ValidMethods: []string{jwt.SigningMethodHS256.Name}}
p := jwt.Parser{ValidMethods: []string{jwt.SigningMethodHS256.Name, jwt.SigningMethodRS256.Name}}
token, err := p.ParseWithClaims(bearer, &GatewayClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(config.JWT.Secret), nil
signMethod := config.JWT.Method
if signMethod == "" {
signMethod = "HS256" // don't break backwards compatibility
}

switch signMethod {
case "HS256":
return []byte(config.JWT.Secret), nil
case "RS256":
return loadRSAPublicKeyFromDisk(config.JWT.Keyfile), nil
default:
return nil, unauthorizedError("Invalid Signing Method: %s", signMethod)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

token.Method will give you the signing method the JWT uses. Consider also checking if that matches - error messages for mismatch conditions would be great.
Please also check if Keyfile or Secret are empty and return an error if they should be present.

})
if err != nil {
return nil, unauthorizedError("Invalid token: %v", err)
}

return withToken(r.Context(), token), nil
}

func loadRSAPublicKeyFromDisk(location string) *rsa.PublicKey {
keyData, e := ioutil.ReadFile(location)
if e != nil {
panic(e.Error())
}
key, e := jwt.ParseRSAPublicKeyFromPEM(keyData)
if e != nil {
panic(e.Error())
}
return key
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not panic on errors. They should always be returned from the function so the caller can decide to handle errors.

4 changes: 3 additions & 1 deletion conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ type DBConfiguration struct {

// JWTConfiguration holds all the JWT related configuration.
type JWTConfiguration struct {
Secret string `json:"secret" required:"true"`
Method string `envconfig:"SIGNING_METHOD" json:"method"`
Secret string `envconfig:"SECRET" json:"secret"`
Keyfile string `envconfig:"KEYFILE" json:"keyfile"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this service has support for a multi instance mode it should be possible to have a different singing key for every instance. The settings of an instance are persisted in the database and therefore cannot be loaded from a file dynamically. It would be great if there also was an option to pass in the PEM-encoded key as a string.

See these files for a similar implementation in gotrue:

}

// GlobalConfiguration holds all the configuration that applies to all instances.
Expand Down
4 changes: 3 additions & 1 deletion example.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
GITGATEWAY_JWT_SECRET="CHANGE-THIS! VERY IMPORTANT!"
GITGATEWAY_JWT_SIGNING_METHOD="HS256" # [HS256|RS256]
GITGATEWAY_JWT_SECRET="CHANGE-THIS! VERY IMPORTANT!" # HS256
#GITGATEWAY_JWT_KEYFILE="/path/to/keyfile.pub" # RS256
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you would like to test for backwards compat, discard this change, so the tests run on a previous config where method did not exist.


GITGATEWAY_DB_DRIVER=sqlite3
DATABASE_URL=gorm.db
Expand Down