github.com/litesolutions/justifay-api@v1.0.0-2.0.20220707114139-46f28a909481/model/oauth.go (about)

     1  package model
     2  
     3  import (
     4  	"database/sql"
     5  	"time"
     6  
     7  	//	"github.com/RichardKnop/go-oauth2-server/util"
     8  	// "github.com/RichardKnop/uuid"
     9  
    10  	//"github.com/jinzhu/gorm"
    11  
    12  	uuid "github.com/google/uuid"
    13  )
    14  
    15  // OauthClient ...
    16  type Client struct {
    17  	IDRecord
    18  	Key                 string         `bun:"type:varchar(254),unique,notnull"`
    19  	Secret              string         `bun:"type:varchar(60),notnull"`
    20  	RedirectURI         sql.NullString `bun:"type:varchar(200)"`
    21  	ApplicationName     sql.NullString `bun:"type:varchar(200)"`
    22  	ApplicationHostname sql.NullString `bun:"type:varchar(200)"`
    23  	ApplicationURL      sql.NullString `bun:"type:varchar(200)"`
    24  }
    25  
    26  // // TableName specifies table name
    27  // func (c *OauthClient) TableName() string {
    28  // 	return "oauth_clients"
    29  // }
    30  
    31  // OauthScope ...
    32  type Scope struct {
    33  	ID          int32  `bun:"type:,unique"`
    34  	Name        string `bun:"type:varchar(50),unique,notnull"`
    35  	Description string `bun:"type:varchar(200)"`
    36  	IsDefault   bool   `bun:"default:false"`
    37  }
    38  
    39  // // TableName specifies table name
    40  // func (s *OauthScope) TableName() string {
    41  // 	return "oauth_scopes"
    42  // }
    43  
    44  // // Role is a one of roles user can have (currently superuser or user)
    45  // type Role struct {
    46  // 	ID          int8   `bun:"primary_key" sql:"type:varchar(20)"`
    47  // 	Name        string `bun:"type:varchar(50),unique,notnull"`
    48  // 	Description string `bun:"type:varchar(200),notnull"`
    49  // }
    50  
    51  // // TableName specifies table name
    52  // func (r *OauthRole) TableName() string {
    53  // 	return "oauth_roles"
    54  // }
    55  
    56  // OauthUser ...
    57  // type OauthUser struct {
    58  // 	RecordBasics
    59  // 	RoleID         sql.NullString `bun:"type:varchar(20);index,notnull"`
    60  // 	Role           *OauthRole
    61  // 	Username       string         `bun:"type:varchar(254),unique,notnull"`
    62  // 	Password       sql.NullString `bun:"type:varchar(60)"`
    63  // 	EmailConfirmed bool           `bun:"default:false,notnull"`
    64  // }
    65  
    66  // // TableName specifies table name
    67  // func (u *OauthUser) TableName() string {
    68  // 	return "oauth_users"
    69  // }
    70  
    71  // OauthRefreshToken ...
    72  type RefreshToken struct {
    73  	IDRecord
    74  	ClientID  uuid.UUID `bun:"type:uuid,notnull"`
    75  	UserID    uuid.UUID `bun:"type:uuid"`
    76  	Client    *Client   `bun:"rel:has-one"`
    77  	User      *User     `bun:"rel:has-one"`
    78  	Token     string    `bun:"type:varchar(40),unique,notnull"`
    79  	ExpiresAt time.Time `bun:",notnull,default:now()"`
    80  	Scope     string    `bun:"type:varchar(200),notnull"`
    81  }
    82  
    83  // // TableName specifies table name
    84  // func (rt *OauthRefreshToken) TableName() string {
    85  // 	return "oauth_refresh_tokens"
    86  // }
    87  
    88  // OauthAccessToken ...
    89  type AccessToken struct {
    90  	IDRecord
    91  	ClientID  uuid.UUID `bun:"type:uuid,notnull"`
    92  	UserID    uuid.UUID `bun:"type:uuid"`
    93  	Client    *Client   `bun:"rel:has-one"`
    94  	User      *User     `bun:"rel:has-one"`
    95  	Token     string    `bun:"type:varchar(40),unique,notnull"`
    96  	ExpiresAt time.Time `bun:",notnull"`
    97  	Scope     string    `bun:"type:varchar(200),notnull"`
    98  }
    99  
   100  // // TableName specifies table name
   101  // func (at *OauthAccessToken) TableName() string {
   102  // 	return "oauth_access_tokens"
   103  // }
   104  
   105  // OauthAuthorizationCode ...
   106  type AuthorizationCode struct {
   107  	IDRecord
   108  	ClientID    uuid.UUID      `bun:"type:uuid,notnull"`
   109  	UserID      uuid.UUID      `bun:"type:uuid,notnull"`
   110  	Client      *Client        `bun:"rel:has-one"`
   111  	User        *User          `bun:"rel:has-one"`
   112  	Code        string         `bun:"type:varchar(40),unique,notnull"`
   113  	RedirectURI sql.NullString `bun:"type:varchar(200)"`
   114  	ExpiresAt   time.Time      `bun:",notnull"`
   115  	Scope       string         `bun:"type:varchar(200),notnull"`
   116  }
   117  
   118  // // TableName specifies table name
   119  // func (ac *OauthAuthorizationCode) TableName() string {
   120  // 	return "oauth_authorization_codes"
   121  // }
   122  
   123  // NewOauthRefreshToken creates new OauthRefreshToken instance
   124  func NewOauthRefreshToken(client *Client, user *User, expiresIn int, scope string) *RefreshToken {
   125  	refreshToken := &RefreshToken{
   126  		IDRecord:  IDRecord{CreatedAt: time.Now().UTC()},
   127  		ClientID:  client.ID,
   128  		Token:     uuid.New().String(),
   129  		ExpiresAt: time.Now().UTC().Add(time.Duration(expiresIn) * time.Second),
   130  		Scope:     scope,
   131  	}
   132  	if user != nil {
   133  		refreshToken.UserID = user.ID
   134  	}
   135  	return refreshToken
   136  }
   137  
   138  // NewOauthAccessToken creates new OauthAccessToken instance
   139  func NewOauthAccessToken(client *Client, user *User, expiresIn int, scope string) *AccessToken {
   140  	accessToken := &AccessToken{
   141  		IDRecord:  IDRecord{CreatedAt: time.Now().UTC()},
   142  		ClientID:  client.ID,
   143  		Token:     uuid.New().String(),
   144  		ExpiresAt: time.Now().UTC().Add(time.Duration(expiresIn) * time.Second),
   145  		Scope:     scope,
   146  	}
   147  	if user != nil {
   148  		accessToken.UserID = user.ID
   149  	}
   150  	return accessToken
   151  }
   152  
   153  // NewOauthAuthorizationCode creates new OauthAuthorizationCode instance
   154  func NewOauthAuthorizationCode(client *Client, user *User, expiresIn int, redirectURI, scope string) *AuthorizationCode {
   155  	return &AuthorizationCode{
   156  		IDRecord:    IDRecord{CreatedAt: time.Now().UTC()},
   157  		ClientID:    client.ID,
   158  		UserID:      user.ID,
   159  		Code:        uuid.New().String(),
   160  		ExpiresAt:   time.Now().UTC().Add(time.Duration(expiresIn) * time.Second),
   161  		RedirectURI: StringOrNull(redirectURI),
   162  		Scope:       scope,
   163  	}
   164  }
   165  
   166  // OauthAuthorizationCodePreload sets up Gorm preloads for an auth code object
   167  // func OauthAuthorizationCodePreload(db *bun.DB) *bun.DB {
   168  // 	return OauthAuthorizationCodePreloadWithPrefix(db, "")
   169  // }
   170  
   171  // // OauthAuthorizationCodePreloadWithPrefix sets up Gorm preloads for an auth code object,
   172  // // and prefixes with prefix for nested objects
   173  // func OauthAuthorizationCodePreloadWithPrefix(db *bun.DB, prefix string) *bun.DB {
   174  // 	return db.
   175  // 		Preload(prefix + "Client").Preload(prefix + "User")
   176  // }
   177  
   178  // // OauthAccessTokenPreload sets up Gorm preloads for an access token object
   179  // func OauthAccessTokenPreload(db *bun.DB) *bun.DB {
   180  // 	return OauthAccessTokenPreloadWithPrefix(db, "")
   181  // }
   182  
   183  // OauthAccessTokenPreload sets up Gorm preloads for an access token object
   184  // func OauthAccessTokenPreload(db *bun.DB) *bun.DB {
   185  // 	return db.Relation("Client").Relation("User")
   186  // }
   187  
   188  // OauthAccessTokenPreloadWithPrefix sets up Gorm preloads for an access token object,
   189  // and prefixes with prefix for nested objects
   190  // func OauthAccessTokenPreloadWithPrefix(db *bun.DB, prefix string) *bun.DB {
   191  // 	return db.
   192  // 		Preload(prefix + "Client").Preload(prefix + "User")
   193  // }
   194  
   195  // // OauthRefreshTokenPreload sets up Gorm preloads for a refresh token object
   196  // func OauthRefreshTokenPreload(db *bun.DB) *bun.DB {
   197  // 	return OauthRefreshTokenPreloadWithPrefix(db, "")
   198  // }
   199  
   200  // // OauthRefreshTokenPreloadWithPrefix sets up Gorm preloads for a refresh token object,
   201  // // and prefixes with prefix for nested objects
   202  // func OauthRefreshTokenPreloadWithPrefix(db *bun.DB, prefix string) *bun.DB {
   203  // 	return db.
   204  // 		Preload(prefix + "Client").Preload(prefix + "User")
   205  // }
   206  
   207  // StringOrNull returns properly configured sql.NullString
   208  func StringOrNull(str string) sql.NullString {
   209  	if str == "" {
   210  		return sql.NullString{String: "", Valid: false}
   211  	}
   212  	return sql.NullString{String: str, Valid: true}
   213  }