github.com/Richardknop/go-oauth2-server@v1.0.1/models/oauth.go (about)

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