go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/apputil/types.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package apputil
     9  
    10  import (
    11  	"fmt"
    12  	"strings"
    13  	"time"
    14  
    15  	"go.charczuk.com/sdk/db"
    16  	"go.charczuk.com/sdk/oauth"
    17  	"go.charczuk.com/sdk/uuid"
    18  	"go.charczuk.com/sdk/web"
    19  )
    20  
    21  // ApplyOAuthProfileToUser applies an oauth proflie.
    22  func ApplyOAuthProfileToUser(u *User, p oauth.Profile) {
    23  	u.Email = p.Email
    24  	u.GivenName = p.GivenName
    25  	u.FamilyName = p.FamilyName
    26  	u.Locale = p.Locale
    27  	u.PictureURL = p.PictureURL
    28  }
    29  
    30  // User is a user
    31  type User struct {
    32  	ID           uuid.UUID `db:"id,pk"`
    33  	CreatedUTC   time.Time `db:"created_utc"`
    34  	LastLoginUTC time.Time `db:"last_login_utc"`
    35  	LastSeenUTC  time.Time `db:"last_seen_utc"`
    36  	ProfileID    string    `db:"profile_id"`
    37  	GivenName    string    `db:"given_name"`
    38  	FamilyName   string    `db:"family_name"`
    39  	PictureURL   string    `db:"picture_url"`
    40  	Locale       string    `db:"locale"`
    41  	Email        string    `db:"email"`
    42  }
    43  
    44  // TableName returns the mapped table name.
    45  func (u User) TableName() string { return "users" }
    46  
    47  // Username returns the user section of the email.
    48  func (u User) Username() string {
    49  	username, _, found := strings.Cut(u.Email, "@")
    50  	if found {
    51  		return username
    52  	}
    53  	return u.Email
    54  }
    55  
    56  // NewTestUser returns a test user.
    57  func NewTestUser() User {
    58  	return User{
    59  		ID:           uuid.V4(),
    60  		CreatedUTC:   time.Now().UTC().Add(-time.Hour),
    61  		LastLoginUTC: time.Now().UTC().Add(-time.Minute),
    62  		LastSeenUTC:  time.Now().UTC().Add(-time.Second),
    63  		ProfileID:    uuid.V4().String(),
    64  		GivenName:    uuid.V4().String(),
    65  		FamilyName:   uuid.V4().String(),
    66  		PictureURL:   fmt.Sprintf("https://example.com/%s.jpg", uuid.V4().String()),
    67  		Locale:       "en-us",
    68  		Email:        fmt.Sprintf("%s@example.com", uuid.V4().String()),
    69  	}
    70  }
    71  
    72  var (
    73  	_ db.TableNameProvider = (*Session)(nil)
    74  )
    75  
    76  // Session holds session information for a user.
    77  type Session struct {
    78  	SessionID   string    `db:"session_id,pk"`
    79  	UserID      uuid.UUID `db:"user_id"`
    80  	BaseURL     string    `db:"base_url"`
    81  	CreatedUTC  time.Time `db:"created_utc"`
    82  	ExpiresUTC  time.Time `db:"expires_utc"`
    83  	LastSeenUTC time.Time `db:"last_seen_utc"`
    84  	UserAgent   string    `db:"user_agent"`
    85  	RemoteAddr  string    `db:"remote_addr"`
    86  	Locale      string    `db:"locale"`
    87  }
    88  
    89  // TableName implements db.TableNameProvider.
    90  func (s Session) TableName() string { return "sessions" }
    91  
    92  // IsZero returns if the object is set or not.
    93  // It will return true if either the userID or the sessionID are unset.
    94  func (s *Session) IsZero() bool {
    95  	return len(s.UserID) == 0 || len(s.SessionID) == 0
    96  }
    97  
    98  // ApplyTo sets a web session from the values on the type session.
    99  func (s *Session) ApplyTo(webSession *web.Session) {
   100  	webSession.SessionID = s.SessionID
   101  	webSession.UserID = s.UserID.String()
   102  	webSession.BaseURL = s.BaseURL
   103  	webSession.CreatedUTC = s.CreatedUTC
   104  	webSession.ExpiresUTC = s.ExpiresUTC
   105  	webSession.UserAgent = s.UserAgent
   106  	webSession.RemoteAddr = s.RemoteAddr
   107  	webSession.Locale = s.Locale
   108  }
   109  
   110  // NewTestSession returns a test session.
   111  func NewTestSession(user *User) Session {
   112  	return Session{
   113  		SessionID:   web.NewSessionID(),
   114  		UserID:      user.ID,
   115  		BaseURL:     "http://localhost",
   116  		CreatedUTC:  time.Now().UTC(),
   117  		ExpiresUTC:  time.Now().UTC().Add(24 * time.Hour),
   118  		LastSeenUTC: time.Now().UTC().Add(time.Hour),
   119  		UserAgent:   "go-test",
   120  		RemoteAddr:  "127.0.0.1",
   121  		Locale:      "en-us",
   122  	}
   123  }
   124  
   125  // SessionState is the session state type.
   126  type SessionState struct {
   127  	User *User
   128  }