github.com/crewjam/saml@v0.4.14/samlsp/session.go (about) 1 package samlsp 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 8 "github.com/crewjam/saml" 9 ) 10 11 // Session is an interface implemented to contain a session. 12 type Session interface{} 13 14 // SessionWithAttributes is a session that can expose the 15 // attributes provided by the SAML identity provider. 16 type SessionWithAttributes interface { 17 Session 18 GetAttributes() Attributes 19 } 20 21 // ErrNoSession is the error returned when the remote user does not have a session 22 var ErrNoSession = errors.New("saml: session not present") 23 24 // SessionProvider is an interface implemented by types that can track 25 // the active session of a user. The default implementation is CookieSessionProvider 26 type SessionProvider interface { 27 // CreateSession is called when we have received a valid SAML assertion and 28 // should create a new session and modify the http response accordingly, e.g. by 29 // setting a cookie. 30 CreateSession(w http.ResponseWriter, r *http.Request, assertion *saml.Assertion) error 31 32 // DeleteSession is called to modify the response such that it removed the current 33 // session, e.g. by deleting a cookie. 34 DeleteSession(w http.ResponseWriter, r *http.Request) error 35 36 // GetSession returns the current Session associated with the request, or 37 // ErrNoSession if there is no valid session. 38 GetSession(r *http.Request) (Session, error) 39 } 40 41 // SessionCodec is an interface to convert SAML assertions to a 42 // Session. The default implementation uses JWTs, JWTSessionCodec. 43 type SessionCodec interface { 44 // New creates a Session from the SAML assertion. 45 New(assertion *saml.Assertion) (Session, error) 46 47 // Encode returns a serialized version of the Session. 48 // 49 // Note: When implementing this function, it is reasonable to expect that 50 // Session is of the exact type returned by New(), and panic if it is not. 51 Encode(s Session) (string, error) 52 53 // Decode parses the serialized session that may have been returned by Encode 54 // and returns a Session. 55 Decode(string) (Session, error) 56 } 57 58 type indexType int 59 60 const sessionIndex indexType = iota 61 62 // SessionFromContext returns the session associated with ctx, or nil 63 // if no session are associated 64 func SessionFromContext(ctx context.Context) Session { 65 v := ctx.Value(sessionIndex) 66 if v == nil { 67 return nil 68 } 69 return v.(Session) 70 } 71 72 // ContextWithSession returns a new context with session associated 73 func ContextWithSession(ctx context.Context, session Session) context.Context { 74 return context.WithValue(ctx, sessionIndex, session) 75 } 76 77 // AttributeFromContext is a convenience method that returns the named attribute 78 // from the session, if available. 79 func AttributeFromContext(ctx context.Context, name string) string { 80 s := SessionFromContext(ctx) 81 if s == nil { 82 return "" 83 } 84 sa, ok := s.(SessionWithAttributes) 85 if !ok { 86 return "" 87 } 88 return sa.GetAttributes().Get(name) 89 }