Skip to content

Commit 151ec09

Browse files
committed
ORGANIC-443. !!Hacks introduced (not for production)!! Reimplemented jwt validation with Okta's golang lib. Made a few hacks to get it working.
1 parent 46b42ae commit 151ec09

File tree

5 files changed

+108
-50
lines changed

5 files changed

+108
-50
lines changed

api/auth.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
"net/http"
66

7-
jwt "github.com/dgrijalva/jwt-go"
87
"github.com/sirupsen/logrus"
8+
"github.com/okta/okta-jwt-verifier-golang"
99
)
1010

1111
// requireAuthentication checks incoming requests for tokens presented using the Authorization header
@@ -35,14 +35,35 @@ func (a *API) extractBearerToken(w http.ResponseWriter, r *http.Request) (string
3535
}
3636

3737
func (a *API) parseJWTClaims(bearer string, r *http.Request) (context.Context, error) {
38+
// Reimplemented to use Okta lib
39+
// Original validation only work for HS256 algo,
40+
// Okta supports RS256 only which requires public key downloading and caching (key rotation)
3841
config := getConfig(r.Context())
39-
p := jwt.Parser{ValidMethods: []string{jwt.SigningMethodHS256.Name}}
40-
token, err := p.ParseWithClaims(bearer, &GatewayClaims{}, func(token *jwt.Token) (interface{}, error) {
41-
return []byte(config.JWT.Secret), nil
42-
})
42+
43+
toValidate := map[string]string{}
44+
toValidate["aud"] = config.JWT.AUD
45+
toValidate["cid"] = config.JWT.CID
46+
47+
jwtVerifierSetup := jwtverifier.JwtVerifier{
48+
Issuer: config.JWT.Issuer,
49+
ClaimsToValidate: toValidate,
50+
}
51+
52+
verifier := jwtVerifierSetup.New()
53+
54+
_, err := verifier.VerifyAccessToken(bearer)
55+
56+
// @TODO? WARNING: Should be roles and other claims be checked here?
57+
4358
if err != nil {
4459
return nil, unauthorizedError("Invalid token: %v", err)
4560
}
4661

47-
return withToken(r.Context(), token), nil
62+
logrus.Infof("parseJWTClaims passed")
63+
64+
// return nil, because the `github.go` is coded to send personal token
65+
// both github oauth generates its own id, so oauth pass-thru is impossible
66+
// we can improve the gateway to talk oauth with github.com, but we will
67+
// still return nil here.
68+
return nil, nil
4869
}

api/github.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ func (gh *GitHubGateway) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7474
}
7575
ctx = withProxyTarget(ctx, target)
7676
ctx = withAccessToken(ctx, config.GitHub.AccessToken)
77+
78+
log := getLogEntry(r)
79+
log.Infof("proxy.ServeHTTP: %+v\n", r.WithContext(ctx))
7780
gh.proxy.ServeHTTP(w, r.WithContext(ctx))
7881
}
7982

@@ -82,8 +85,12 @@ func (gh *GitHubGateway) authenticate(w http.ResponseWriter, r *http.Request) er
8285
claims := getClaims(ctx)
8386
config := getConfig(ctx)
8487

88+
log := getLogEntry(r)
89+
log.Infof("authenticate context: %v+", ctx)
8590
if claims == nil {
86-
return errors.New("Access to endpoint not allowed: no claims found in Bearer token")
91+
// @TODO? WARNING: the check should be done in auth.go, imo.
92+
// Having the jwt in the context (and thus, sent to github.com) is not necessary
93+
// return errors.New("Access to endpoint not allowed: no claims found in Bearer token")
8794
}
8895

8996
if !allowedRegexp.MatchString(r.URL.Path) {

conf/configuration.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ type DBConfiguration struct {
4646
// JWTConfiguration holds all the JWT related configuration.
4747
type JWTConfiguration struct {
4848
Secret string `json:"secret" required:"true"`
49+
CID string `envconfig:"CLIENT_ID" json:"client_id,omitempty"`
50+
Issuer string `envconfig:"ISSUER" json:"issuer,omitempty"`
51+
AUD string `envconfig:"AUD" json:"aud,omitempty"`
4952
}
5053

5154
// GlobalConfiguration holds all the configuration that applies to all instances.

glide.lock

Lines changed: 68 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package: github.com/netlify/git-gateway
22
import:
33
- package: github.com/dgrijalva/jwt-go
44
version: v3.0.0
5+
- package: github.com/okta/okta-jwt-verifier-golang
6+
version: 04702def3e1b9b1c6b419c9c3aae1ec184a5d4b2
57
- package: github.com/jinzhu/gorm
68
version: 5b8c0dd6b92d9caa8036c31dcb117f2df7cceefa
79
- package: github.com/pborman/uuid

0 commit comments

Comments
 (0)