Skip to content

Commit 1ec6180

Browse files
author
Andrew Heberle
committed
Add cookie name
1 parent 665354e commit 1ec6180

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ AUTH_IDP_METADATA=https://idp.example.net/metadata \
4646
--listen string Listen address (default "127.0.0.1:9091")
4747
--sp-cert string Service Provider Certificate
4848
--sp-claim-mapping stringToString Mapping of claims to headers (default [remote-user=urn:oasis:names:tc:SAML:attribute:subject-id,remote-email=mail,remote-name=displayName,remote-groups=role])
49+
--sp-cookie Cookie Name set by Service Provider (default "token")
4950
--sp-key string Service Provider Key
5051
--sp-url string Service Provider URL (default "http://localhost:9091")
5152
```
@@ -60,10 +61,16 @@ For this reason, the token only contains minimal data with the rest contained se
6061

6162
By default this store is a basic in-memory store, which means it cannot be shared among multiple instances of this service and also will be lost on restart. The loss of this data on restart is not particularly problematic as the only result will be that the SP will not be able to validate the user is signed in and force the login flow to the IdP.
6263

64+
If using muliple nodes however, using the in-memory store will cause unexpected re-authentiations if requests are handled by different instances.
65+
6366
When using multiple instances, it is possible to use a PostgreSQL database to store this content.
6467

6568
### Using the same database for multiple deployments
6669

6770
All instances of the same Service Provider should share the same configuration options, including the database store, however if seperate service providers are configured using the same database there is the chance incorrect claims may be returned.
6871

6972
To allow sharing of the same database between seperate Service Providers, the `db-prefix` option will ensure this data is stored in seperate tables.
73+
74+
### Cookie Name
75+
76+
The login "token" is stored as a JWT in a cookie named "token" by default. It is important to ensure that seperate SP's use distinct cookie names to ensure JWT's are correctly validated and not overwritten.

internal/pkg/cmd/cmd.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type rootCommand struct {
2828
debug bool
2929

3030
// sp flags
31+
spCookie string
3132
spCert string
3233
spKey string
3334
spUrl string
@@ -61,6 +62,7 @@ func (c *rootCommand) Init(cd *simplecobra.Commandeer) error {
6162
cmd.Flags().BoolVar(&c.debug, "debug", false, "Enable debug logging")
6263

6364
// sp command line flags
65+
cmd.Flags().StringVar(&c.spCookie, "sp-cookie", "token", "Cookie Name set by Service Provider")
6466
cmd.Flags().StringVar(&c.spCert, "sp-cert", "", "Service Provider Certificate")
6567
cmd.Flags().StringVar(&c.spKey, "sp-key", "", "Service Provider Key")
6668
cmd.MarkFlagsRequiredTogether("sp-cert", "sp-key")
@@ -107,6 +109,7 @@ type serviceProvider struct {
107109
ServiceProviderClaimMapping map[string]string `mapstructure:"sp-claim-mapping"`
108110
ServiceProviderCertificate string `mapstructure:"sp-cert"`
109111
ServiceProviderKey string `mapstructure:"sp-key"`
112+
ServiceProviderCookieName string `mapstructure:"sp-cookie"`
110113
IdPMetadata string `mapstructure:"idp-metadata"`
111114
IdPIssuer string `mapstructure:"idp-issuer"`
112115
IdPSSOEndpoint string `mapstructure:"idp-sso-endpoint"`
@@ -127,6 +130,7 @@ func (c *rootCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args
127130
// use global values as a fallback if some values are not set
128131
spConfig.ServiceProviderCertificate = fallback(spConfig.ServiceProviderCertificate, c.spCert)
129132
spConfig.ServiceProviderKey = fallback(spConfig.ServiceProviderKey, c.spKey)
133+
spConfig.ServiceProviderCookieName = fallback(spConfig.ServiceProviderCookieName, c.spCookie)
130134

131135
// show config in debug mode
132136
c.logger.Debug("setting up service provider",
@@ -146,6 +150,7 @@ func (c *rootCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args
146150
// set up service provider options
147151
opts := []sp.ServiceProviderOption{
148152
sp.WithClaimMapping(spConfig.ServiceProviderClaimMapping),
153+
sp.WithCookieName(spConfig.ServiceProviderCookieName),
149154
}
150155

151156
// handle metadata
@@ -302,6 +307,7 @@ func (c *rootCommand) serviceProviders() []serviceProvider {
302307
ServiceProviderClaimMapping: c.spClaimMapping,
303308
ServiceProviderCertificate: c.spCert,
304309
ServiceProviderKey: c.spKey,
310+
ServiceProviderCookieName: c.spCookie,
305311
IdPMetadata: c.idpMetadata,
306312
IdPIssuer: c.idpIssuer,
307313
IdPSSOEndpoint: c.idpSSOEndpoint,
@@ -320,6 +326,7 @@ func (c *rootCommand) serviceProviders() []serviceProvider {
320326
ServiceProviderClaimMapping: c.spClaimMapping,
321327
ServiceProviderCertificate: c.spCert,
322328
ServiceProviderKey: c.spKey,
329+
ServiceProviderCookieName: c.spCookie,
323330
IdPMetadata: c.idpMetadata,
324331
IdPIssuer: c.idpIssuer,
325332
IdPSSOEndpoint: c.idpSSOEndpoint,

pkg/sp/options.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ func WithMetadataRefreshInterval(d time.Duration) ServiceProviderOption {
6767
}
6868
}
6969

70+
func WithCookieName(name string) ServiceProviderOption {
71+
return func(s *ServiceProvider) {
72+
s.cookieName = name
73+
}
74+
}
75+
7076
func WithName(name string) ServiceProviderOption {
7177
return func(s *ServiceProvider) {
7278
s.name = name

pkg/sp/sp.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type ServiceProvider struct {
2929
store AttributeStore
3030
opts samlsp.Options
3131
name string
32+
cookieName string
3233
onerror func(w http.ResponseWriter, r *http.Request, err error)
3334
}
3435

@@ -77,6 +78,7 @@ func NewServiceProvider(cert, key string, root *url.URL, options ...ServiceProvi
7778
EntityID: root.String(),
7879
Key: keyPair.PrivateKey.(*rsa.PrivateKey),
7980
Certificate: keyPair.Leaf,
81+
CookieName: serviceProvider.cookieName,
8082
IDPMetadata: serviceProvider.idpMetadata,
8183
AllowIDPInitiated: true,
8284
SignRequest: true,

pkg/sp/tracker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func DefaultRequestTracker(opts samlsp.Options, serviceProvider *saml.ServiceProvider) CookieRequestTracker {
1616
return CookieRequestTracker{
1717
ServiceProvider: serviceProvider,
18-
NamePrefix: "saml_",
18+
NamePrefix: fmt.Sprintf("saml_%s_", opts.CookieName),
1919
Codec: samlsp.DefaultTrackedRequestCodec(opts),
2020
MaxAge: saml.MaxIssueDelay,
2121
RelayStateFunc: opts.RelayStateFunc,

0 commit comments

Comments
 (0)