-
Notifications
You must be signed in to change notification settings - Fork 89
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ package api | |
|
||
import ( | ||
"context" | ||
"crypto/rsa" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
jwt "github.com/dgrijalva/jwt-go" | ||
|
@@ -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) | ||
} | ||
}) | ||
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 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
// GlobalConfiguration holds all the configuration that applies to all instances. | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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.