Skip to content

Commit 76f77fe

Browse files
committed
ORGANIC-467. Added the option to specify Authenticator to use in env.
1 parent 163a708 commit 76f77fe

File tree

4 files changed

+47
-27
lines changed

4 files changed

+47
-27
lines changed

api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func NewAPI(globalConfig *conf.GlobalConfiguration, db storage.Connection) *API
5959

6060
// NewAPIWithVersion creates a new REST API using the specified version
6161
func NewAPIWithVersion(ctx context.Context, globalConfig *conf.GlobalConfiguration, db storage.Connection, version string) *API {
62-
auth := NewAuthWithVersion(ctx, globalConfig, version)
62+
auth := NewAuthWithVersion(ctx, version)
6363
api := &API{config: globalConfig, db: db, auth: *auth, version: version}
6464

6565
xffmw, _ := xff.Default()

api/auth.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,23 @@ import (
55
"net/http"
66

77
jwt "github.com/dgrijalva/jwt-go"
8-
"github.com/netlify/git-gateway/conf"
98
"github.com/sirupsen/logrus"
109
"github.com/okta/okta-jwt-verifier-golang"
1110
)
1211

1312
type Authenticator interface {
14-
// authenticate checks incoming requests for tokens presented using the Authorization header
13+
// `authenticate` checks incoming requests for tokens presented using the Authorization header
1514
authenticate(w http.ResponseWriter, r *http.Request) (context.Context, error)
1615
getName() string
1716
}
1817

1918
type Authorizer interface {
20-
// authorize checks incoming requests for roles data in tokens that is parsed and verified by prior authentication step
19+
// `authorize` checks incoming requests for roles data in tokens that is parsed and verified by a prior `authenticate` step
2120
authorize(w http.ResponseWriter, r *http.Request) (context.Context, error)
2221
getName() string
2322
}
2423

2524
type Auth struct {
26-
config *conf.GlobalConfiguration
2725
authenticator Authenticator
2826
authorizer Authorizer
2927
version string
@@ -44,10 +42,23 @@ type RolesAuthorizer struct {
4442
auth Auth
4543
}
4644

47-
func NewAuthWithVersion(ctx context.Context, globalConfig *conf.GlobalConfiguration, version string) *Auth {
48-
auth := &Auth{config: globalConfig, version: version}
45+
func NewAuthWithVersion(ctx context.Context, version string) *Auth {
46+
config := getConfig(ctx)
47+
auth := &Auth{version: version}
48+
authenticatorName := config.JWT.Authenticator
49+
50+
if (authenticatorName == "bearer-jwt-token") {
51+
auth.authenticator = &JWTAuthenticator{name: "bearer-jwt-token", auth: *auth}
52+
} else if (authenticatorName == "bearer-okta-jwt-token") {
53+
auth.authenticator = &OktaJWTAuthenticator{name: "bearer-okta-jwt-token", auth: *auth}
54+
} else {
55+
if (authenticatorName != "") {
56+
logrus.Fatal("Authenticator `%v` is not recognized", authenticatorName)
57+
} else {
58+
logrus.Fatal("Authenticator is not defined")
59+
}
60+
}
4961

50-
auth.authenticator = &OktaJWTAuthenticator{name: "bearer-jwt-token", auth: *auth}
5162
auth.authorizer = &RolesAuthorizer{name: "bearer-jwt-token-roles", auth: *auth}
5263

5364
return auth

conf/configuration.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type JWTConfiguration struct {
4949
CID string `envconfig:"CLIENT_ID" json:"client_id,omitempty"`
5050
Issuer string `envconfig:"ISSUER" json:"issuer,omitempty"`
5151
AUD string `envconfig:"AUD" json:"aud,omitempty"`
52+
Authenticator string `envconfig:"AUTHENTICATOR" json:"authenticator,omitempty"`
5253
}
5354

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

example.env

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
1-
# Warning: Many configuration would not work with quote (ie "").
1+
# Do not use quote (ie, "") if you start `git-gateway` with docker command: `--env-file`
22

3-
# JWT Secret is not needed for RS256. Instead, issuer should be specified
4-
# (eg, https://dev-1234.oktapreview.com/oauth2/default)
5-
GITGATEWAY_JWT_SECRET=
3+
# DB
4+
GITGATEWAY_DB_DRIVER=sqlite3
5+
DATABASE_URL=gorm.db
66

7-
# @TODO - REQUIRED for Okta
8-
ISSUER=
7+
# Startup Options
8+
GITGATEWAY_API_HOST=localhost
9+
PORT=9999
910

10-
# @TODO - REQUIRED for Okta
11-
CLIENT_ID=
11+
# <> config for JWT Token with HS256 alg
12+
# AUTHENTICATOR=bearer-jwt-token
13+
14+
# Leave blank for other AUTHENTICATOR
15+
GITGATEWAY_JWT_SECRET="CHANGE-THIS! VERY IMPORTANT!"
16+
# </>
1217

18+
# <> config for JWT Token with Okta (RS256) alg
19+
AUTHENTICATOR=bearer-okta-jwt-token
20+
21+
# REQUIRED for AUTHENTICATOR=bearer-okta-jwt-token
1322
AUD=api://default
1423

15-
GITGATEWAY_DB_DRIVER=sqlite3
16-
DATABASE_URL=gorm.db
24+
# REQUIRED for AUTHENTICATOR=bearer-okta-jwt-token
25+
ISSUER=
26+
# </>
1727

18-
# @TODO - Is there way to expose internal port from Docker?
19-
GITGATEWAY_API_HOST=0.0.0.0
20-
PORT=8087
28+
# REQUIRED for both AUTHENTICATOR = {bearer-jwt-token or bearer-okta-jwt-token}
29+
CLIENT_ID=
2130

22-
# @TODO - REQUIRED
31+
# REQUIRED for GITHUB
2332
GITGATEWAY_GITHUB_ACCESS_TOKEN=
2433

25-
# @TODO - REQUIRED
26-
GITGATEWAY_GITHUB_REPO=
34+
# REQUIRED for GITHUB
35+
GITGATEWAY_GITHUB_REPO=owner/name
2736

28-
# Original example.env wrote: leave blank to allow all roles. But, it won't
29-
# work unless it is commented out
30-
# GITGATEWAY_ROLES=
37+
# Commented out to allow roles
38+
GITGATEWAY_ROLES=admin,cms

0 commit comments

Comments
 (0)