1 /* 2 * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 * @author Aeneas Rekkas <aeneas+oss@aeneas.io> 17 * @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> 18 * @license Apache-2.0 19 * 20 */ 21 22 package fosite 23 24 import ( 25 "time" 26 27 "github.com/mohae/deepcopy" 28 ) 29 30 // Session is an interface that is used to store session data between OAuth2 requests. It can be used to look up 31 // when a session expires or what the subject's name was. 32 type Session interface { 33 // SetExpiresAt sets the expiration time of a token. 34 // 35 // session.SetExpiresAt(fosite.AccessToken, time.Now().UTC().Add(time.Hour)) 36 SetExpiresAt(key TokenType, exp time.Time) 37 38 // GetExpiresAt returns the expiration time of a token if set, or time.IsZero() if not. 39 // 40 // session.GetExpiresAt(fosite.AccessToken) 41 GetExpiresAt(key TokenType) time.Time 42 43 // GetUsername returns the username, if set. This is optional and only used during token introspection. 44 GetUsername() string 45 46 // GetSubject returns the subject, if set. This is optional and only used during token introspection. 47 GetSubject() string 48 49 // Clone clones the session. 50 Clone() Session 51 } 52 53 // DefaultSession is a default implementation of the session interface. 54 type DefaultSession struct { 55 ExpiresAt map[TokenType]time.Time 56 Username string 57 Subject string 58 Extra map[string]interface{} 59 } 60 61 func (s *DefaultSession) SetExpiresAt(key TokenType, exp time.Time) { 62 if s.ExpiresAt == nil { 63 s.ExpiresAt = make(map[TokenType]time.Time) 64 } 65 s.ExpiresAt[key] = exp 66 } 67 68 func (s *DefaultSession) GetExpiresAt(key TokenType) time.Time { 69 if s.ExpiresAt == nil { 70 s.ExpiresAt = make(map[TokenType]time.Time) 71 } 72 73 if _, ok := s.ExpiresAt[key]; !ok { 74 return time.Time{} 75 } 76 return s.ExpiresAt[key] 77 } 78 79 func (s *DefaultSession) GetUsername() string { 80 if s == nil { 81 return "" 82 } 83 return s.Username 84 } 85 86 func (s *DefaultSession) SetSubject(subject string) { 87 s.Subject = subject 88 } 89 90 func (s *DefaultSession) GetSubject() string { 91 if s == nil { 92 return "" 93 } 94 95 return s.Subject 96 } 97 98 func (s *DefaultSession) Clone() Session { 99 if s == nil { 100 return nil 101 } 102 103 return deepcopy.Copy(s).(Session) 104 } 105 106 // ExtraClaimsSession provides an interface for session to store any extra claims. 107 type ExtraClaimsSession interface { 108 // GetExtraClaims returns a map to store extra claims. 109 // The returned value can be modified in-place. 110 GetExtraClaims() map[string]interface{} 111 } 112 113 // GetExtraClaims implements ExtraClaimsSession for DefaultSession. 114 // The returned value can be modified in-place. 115 func (s *DefaultSession) GetExtraClaims() map[string]interface{} { 116 if s == nil { 117 return nil 118 } 119 120 if s.Extra == nil { 121 s.Extra = make(map[string]interface{}) 122 } 123 124 return s.Extra 125 } 126