| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | package models | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/pocketbase/pocketbase/tools/security" | 
					
						
							|  |  |  | 	"github.com/pocketbase/pocketbase/tools/types" | 
					
						
							|  |  |  | 	"golang.org/x/crypto/bcrypt" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var ( | 
					
						
							|  |  |  | 	_ Model = (*Admin)(nil) | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type Admin struct { | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 	BaseModel | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 	Avatar          int            `db:"avatar" json:"avatar"` | 
					
						
							|  |  |  | 	Email           string         `db:"email" json:"email"` | 
					
						
							|  |  |  | 	TokenKey        string         `db:"tokenKey" json:"-"` | 
					
						
							|  |  |  | 	PasswordHash    string         `db:"passwordHash" json:"-"` | 
					
						
							|  |  |  | 	LastResetSentAt types.DateTime `db:"lastResetSentAt" json:"-"` | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | // TableName returns the Admin model SQL table name.
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | func (m *Admin) TableName() string { | 
					
						
							|  |  |  | 	return "_admins" | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // ValidatePassword validates a plain password against the model's password.
 | 
					
						
							|  |  |  | func (m *Admin) ValidatePassword(password string) bool { | 
					
						
							|  |  |  | 	bytePassword := []byte(password) | 
					
						
							|  |  |  | 	bytePasswordHash := []byte(m.PasswordHash) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// comparing the password with the hash
 | 
					
						
							|  |  |  | 	err := bcrypt.CompareHashAndPassword(bytePasswordHash, bytePassword) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// nil means it is a match
 | 
					
						
							|  |  |  | 	return err == nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // SetPassword sets cryptographically secure string to `model.Password`.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Additionally this method also resets the LastResetSentAt and the TokenKey fields.
 | 
					
						
							|  |  |  | func (m *Admin) SetPassword(password string) error { | 
					
						
							|  |  |  | 	if password == "" { | 
					
						
							|  |  |  | 		return errors.New("The provided plain password is empty") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// hash the password
 | 
					
						
							| 
									
										
										
										
											2023-03-19 22:10:11 +08:00
										 |  |  | 	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), 12) | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m.PasswordHash = string(hashedPassword) | 
					
						
							|  |  |  | 	m.LastResetSentAt = types.DateTime{} // reset
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// invalidate previously issued tokens
 | 
					
						
							|  |  |  | 	return m.RefreshTokenKey() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // RefreshTokenKey generates and sets new random token key.
 | 
					
						
							|  |  |  | func (m *Admin) RefreshTokenKey() error { | 
					
						
							|  |  |  | 	m.TokenKey = security.RandomString(50) | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |