Skip to content

Commit

Permalink
Make bearer cookie name configurable (#25)
Browse files Browse the repository at this point in the history
With proper configuration this fixes our send.fs.neo.org problem when we
have two containers involved in serving various parts of the website.
  • Loading branch information
roman-khimov authored May 12, 2023
2 parents ca5c555 + 05fcf6e commit fc21247
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ rebalance_timer: 15s
```
| Parameter | Type | Default value | Description |
|-------------------|------------|---------------|----------------------------------------------------------------------------------------------------|
| `bearer_cookie_name`| `string` | `Bearer` | The name of the cookie holding bearer token. |
| `redirect.url` | `string` | | URL to redirect users going through the OAuth flow |
| `listen_address` | `string` | | The address that the app is listening on. |
| `logger.level` | `string` | `debug` | Logging level.<br/>Possible values: `debug`, `info`, `warn`, `error`, `dpanic`, `panic`, `fatal`. |
Expand Down
15 changes: 7 additions & 8 deletions auth/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ type Authenticator struct {

// Config for authenticator handler.
type Config struct {
Bearer *bearer.Config
Oauth map[string]*ServiceOauth
TLSEnabled bool
Host string
RedirectURL string
Bearer *bearer.Config
BearerCookieName string
Oauth map[string]*ServiceOauth
TLSEnabled bool
Host string
RedirectURL string
}

// New creates authenticator using config.
Expand Down Expand Up @@ -104,10 +105,8 @@ func (u *Authenticator) Callback(w http.ResponseWriter, r *http.Request) {
return
}

w.Header().Set("Authorization", "Bearer "+strToken)

http.SetCookie(w, &http.Cookie{
Name: "Bearer",
Name: u.config.BearerCookieName,
Value: strToken,
MaxAge: 600,
})
Expand Down
13 changes: 9 additions & 4 deletions cmd/neofs-oauthz/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ func (a *app) initAuthCfg(key *keys.PrivateKey) {
if len(listenAddress) == 0 {
listenAddress = defaultListenAddress
}
bearerCookieName := a.cfg.GetString(cfgBearerCookieName)
if len(bearerCookieName) == 0 {
bearerCookieName = defaultBearerCookieName
}

a.authCfg = &auth.Config{
Bearer: &bearer.Config{
Expand All @@ -216,10 +220,11 @@ func (a *app) initAuthCfg(key *keys.PrivateKey) {
ContainerID: containerID,
LifeTime: lifetime,
},
Oauth: make(map[string]*auth.ServiceOauth),
TLSEnabled: a.cfg.GetString(cfgTLSCertificate) != "" || a.cfg.GetString(cfgTLSKey) != "",
Host: listenAddress,
RedirectURL: a.cfg.GetString(cfgRedirectURL),
BearerCookieName: bearerCookieName,
Oauth: make(map[string]*auth.ServiceOauth),
TLSEnabled: a.cfg.GetString(cfgTLSCertificate) != "" || a.cfg.GetString(cfgTLSKey) != "",
Host: listenAddress,
RedirectURL: a.cfg.GetString(cfgRedirectURL),
}

redirectURLCallback := fmt.Sprintf(callbackURLFmt, a.authCfg.RedirectURL)
Expand Down
12 changes: 6 additions & 6 deletions cmd/neofs-oauthz/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
)

const (
defaultRebalanceTimer = 15 * time.Second
defaultRequestTimeout = 15 * time.Second
defaultConnectTimeout = 30 * time.Second
defaultBearerLifetime = 30
defaultBearerCookieName = "Bearer"
defaultBearerLifetime = 30
defaultConnectTimeout = 30 * time.Second
defaultRebalanceTimer = 15 * time.Second
defaultRequestTimeout = 15 * time.Second

defaultListenAddress = "0.0.0.0:8083"

Expand All @@ -43,9 +44,8 @@ const (
cmdHelp = "help"
cmdVersion = "version"
cmdConfig = "config"
)

const (
cfgBearerCookieName = "bearer_cookie_name"
cfgOauth = "oauth"
cfgOauthIDFmt = "oauth.%s.id"
cfgOauthSecretFmt = "oauth.%s.secret"
Expand Down
2 changes: 2 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ listen_address: 0.0.0.0:8083
logger:
level: debug

bearer_cookie_name: "Bearer"

connect_timeout: 30s
request_timeout: 15s
rebalance_timer: 15s

0 comments on commit fc21247

Please sign in to comment.