MCPcopy Create free account
hub / github.com/crewjam/saml / Example_sessionMaxAge

Function Example_sessionMaxAge

samlsp/example_test.go:21–51  ·  view source on GitHub ↗

This example shows how to change how long a session lasts. It replaces the CookieMaxAge option that was removed from Options. The session lifetime is set in two places: the MaxAge of the cookie (CookieSessionProvider.MaxAge) and the expiry of the JWT stored in that cookie (JWTSessionCodec.MaxAge).

()

Source from the content-addressed store, hash-verified

19// raised, the browser keeps the cookie but the token in it is rejected once
20// the JWT expires, after the default one hour.
21func Example_sessionMaxAge() {
22 key, err := rsa.GenerateKey(rand.Reader, 2048)
23 if err != nil {
24 panic(err)
25 }
26 rootURL, _ := url.Parse("https://sp.example.com/")
27
28 opts := samlsp.Options{
29 URL: *rootURL,
30 Key: key,
31 // ... and the rest of your options, e.g. Certificate and IDPMetadata
32 }
33
34 sessionMaxAge := 12 * time.Hour
35
36 codec := samlsp.DefaultSessionCodec(opts)
37 codec.MaxAge = sessionMaxAge
38
39 sessionProvider := samlsp.DefaultSessionProvider(opts)
40 sessionProvider.MaxAge = sessionMaxAge
41 sessionProvider.Codec = codec
42
43 middleware, err := samlsp.New(opts)
44 if err != nil {
45 panic(err)
46 }
47 middleware.Session = sessionProvider
48
49 fmt.Println(middleware.Session.(samlsp.CookieSessionProvider).MaxAge)
50 // Output: 12h0m0s
51}

Callers

nothing calls this directly

Calls 4

DefaultSessionCodecFunction · 0.92
DefaultSessionProviderFunction · 0.92
NewFunction · 0.92
PrintlnMethod · 0.80

Tested by

no test coverage detected