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

Method CreateSession

samlsp/session_cookie.go:76–128  ·  view source on GitHub ↗

CreateSession is called when we have received a valid SAML assertion and should create a new session and modify the http response accordingly, e.g. by setting a cookie.

(w http.ResponseWriter, r *http.Request, assertion *saml.Assertion)

Source from the content-addressed store, hash-verified

74// should create a new session and modify the http response accordingly, e.g. by
75// setting a cookie.
76func (c CookieSessionProvider) CreateSession(w http.ResponseWriter, r *http.Request, assertion *saml.Assertion) error {
77 // Cookies should not have the port attached to them so strip it off
78 if domain, _, err := net.SplitHostPort(c.Domain); err == nil {
79 c.Domain = domain
80 }
81
82 session, err := c.Codec.New(assertion)
83 if err != nil {
84 return err
85 }
86
87 // The cookie lasts no longer than the IdP session (SessionNotOnOrAfter).
88 maxAge := c.MaxAge
89 if end, ok := sessionEnd(assertion); ok {
90 remaining := end.Sub(clockNow(c.Clock))
91 if remaining < time.Second {
92 return fmt.Errorf("%w: the IdP session ends at %s (SessionNotOnOrAfter)", ErrSessionEnded, end)
93 }
94 if remaining < maxAge {
95 maxAge = remaining
96 }
97 }
98
99 value, err := c.Codec.Encode(session)
100 if err != nil {
101 return err
102 }
103 if size := len(c.Name) + len(value); size > maxSessionCookieSize {
104 return fmt.Errorf("%w: cookie %q is %d bytes, browsers accept at most %d",
105 ErrSessionCookieTooLarge, c.Name, size, maxSessionCookieSize)
106 }
107
108 path := c.Path
109 if path == "" {
110 path = "/"
111 }
112
113 // Expire the legacy cookie before setting the new one: a browser that
114 // treats both as the same cookie then keeps the new one.
115 c.expireLegacyCookie(w, r, path)
116
117 http.SetCookie(w, &http.Cookie{ //nolint:gosec // Secure is set when served over https
118 Name: c.Name,
119 Domain: c.Domain,
120 Value: value,
121 MaxAge: int(maxAge.Seconds()),
122 HttpOnly: c.HTTPOnly,
123 Secure: c.Secure || r.TLS != nil,
124 SameSite: c.SameSite,
125 Path: path,
126 })
127 return nil
128}
129
130// DeleteSession is called to modify the response such that it removed the current
131// session, e.g. by deleting a cookie.

Calls 5

expireLegacyCookieMethod · 0.95
sessionEndFunction · 0.85
clockNowFunction · 0.70
NewMethod · 0.65
EncodeMethod · 0.65