github.com/blend/go-sdk@v1.20220411.3/web/session.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package web 9 10 import ( 11 "time" 12 ) 13 14 // NewSession returns a new session object. 15 func NewSession(userID string, sessionID string) *Session { 16 return &Session{ 17 UserID: userID, 18 SessionID: sessionID, 19 CreatedUTC: time.Now().UTC(), 20 State: map[string]interface{}{}, 21 } 22 } 23 24 // Session is an active session 25 type Session struct { 26 UserID string `json:"userID" yaml:"userID"` 27 BaseURL string `json:"baseURL" yaml:"baseURL"` 28 SessionID string `json:"sessionID" yaml:"sessionID"` 29 CreatedUTC time.Time `json:"createdUTC" yaml:"createdUTC"` 30 ExpiresUTC time.Time `json:"expiresUTC" yaml:"expiresUTC"` 31 UserAgent string `json:"userAgent" yaml:"userAgent"` 32 RemoteAddr string `json:"remoteAddr" yaml:"remoteAddr"` 33 State map[string]interface{} `json:"state,omitempty" yaml:"state,omitempty"` 34 } 35 36 // WithBaseURL sets the base url. 37 func (s *Session) WithBaseURL(baseURL string) *Session { 38 s.BaseURL = baseURL 39 return s 40 } 41 42 // WithUserAgent sets the user agent. 43 func (s *Session) WithUserAgent(userAgent string) *Session { 44 s.UserAgent = userAgent 45 return s 46 } 47 48 // WithRemoteAddr sets the remote addr. 49 func (s *Session) WithRemoteAddr(remoteAddr string) *Session { 50 s.RemoteAddr = remoteAddr 51 return s 52 } 53 54 // IsExpired returns if the session is expired. 55 func (s *Session) IsExpired() bool { 56 if s.ExpiresUTC.IsZero() { 57 return false 58 } 59 return s.ExpiresUTC.Before(time.Now().UTC()) 60 } 61 62 // IsZero returns if the object is set or not. 63 // It will return true if either the userID or the sessionID are unset. 64 func (s *Session) IsZero() bool { 65 return len(s.UserID) == 0 || len(s.SessionID) == 0 66 }