Private
Public Access
1
0

Authentication: Implements authentication

This commit is contained in:
2023-06-18 13:11:51 -07:00
parent 53870e25a9
commit 6bbcf8cc63
8 changed files with 302 additions and 10 deletions

View File

@@ -7,9 +7,23 @@ import (
const VERSION = "Kordophone-2.0"
const (
AUTH_USERNAME = "test"
AUTH_PASSWORD = "test"
)
type Server struct {
version string
conversations []model.Conversation
authTokens []model.AuthToken
}
type AuthError struct {
message string
}
func (e *AuthError) Error() string {
return e.message
}
func NewServer() *Server {
@@ -40,3 +54,39 @@ func (s *Server) PopulateWithTestData() {
s.conversations = cs
}
func (s *Server) Authenticate(username string, password string) (*model.AuthToken, error) {
if username != AUTH_USERNAME || password != AUTH_PASSWORD {
return nil, &AuthError{"Invalid username or password"}
}
token, err := model.NewAuthToken(username)
if err != nil {
return nil, err
}
// Register for future auth
s.registerAuthToken(token)
return token, nil
}
func (s *Server) CheckBearerToken(token string) bool {
return s.authenticateToken(token)
}
// Private
func (s *Server) registerAuthToken(token *model.AuthToken) {
s.authTokens = append(s.authTokens, *token)
}
func (s *Server) authenticateToken(token string) bool {
for _, t := range s.authTokens {
if t.SignedToken == token {
return true
}
}
return false
}