Authentication: Implements authentication
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user