Skip to content

Commit

Permalink
Reorganized auth module
Browse files Browse the repository at this point in the history
  • Loading branch information
p0t4t0sandwich committed Apr 19, 2024
1 parent e4267a2 commit 13843da
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 62 deletions.
4 changes: 2 additions & 2 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"time"

"github.com/NeuralNexusDev/neuralnexus-api/modules/auth"
sess "github.com/NeuralNexusDev/neuralnexus-api/modules/auth/session"
"github.com/NeuralNexusDev/neuralnexus-api/modules/database"
"github.com/NeuralNexusDev/neuralnexus-api/responses"
"github.com/google/uuid"
Expand Down Expand Up @@ -70,7 +70,7 @@ func Auth(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
db := database.GetDB("neuralnexus")
rdb := database.GetRedis()
sessService := auth.NewSessionService(auth.NewSessionStore(db, rdb))
sessService := sess.NewSessionService(sess.NewSessionStore(db, rdb))

authHeader := r.Header.Get("Authorization")
if authHeader == "" {
Expand Down
3 changes: 2 additions & 1 deletion modules/auth/linking/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/NeuralNexusDev/neuralnexus-api/modules/auth"
sess "github.com/NeuralNexusDev/neuralnexus-api/modules/auth/session"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -203,7 +204,7 @@ func GetDiscordUser(accessToken string) (*DiscordData, error) {
}

// DiscordOAuth process the Discord OAuth flow
func DiscordOAuth(as auth.AccountStore, ss auth.SessionStore, las LinkAccountStore, code, state string) (*auth.Session, error) {
func DiscordOAuth(as auth.AccountStore, ss sess.SessionStore, las LinkAccountStore, code, state string) (*sess.Session, error) {
var a *auth.Account
// TODO: Sign the state so it can't be tampered with/impersonated
if state != "" && false { // TEMPORARILY DISABLED
Expand Down
2 changes: 1 addition & 1 deletion modules/auth/rbac.go → modules/auth/permissions/rbac.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package auth
package perms

import "errors"

Expand Down
11 changes: 6 additions & 5 deletions modules/auth/routes/authroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
mw "github.com/NeuralNexusDev/neuralnexus-api/middleware"
"github.com/NeuralNexusDev/neuralnexus-api/modules/auth"
accountlinking "github.com/NeuralNexusDev/neuralnexus-api/modules/auth/linking"
sess "github.com/NeuralNexusDev/neuralnexus-api/modules/auth/session"
"github.com/NeuralNexusDev/neuralnexus-api/modules/database"
"github.com/NeuralNexusDev/neuralnexus-api/responses"
)
Expand All @@ -19,7 +20,7 @@ func ApplyRoutes(mux *http.ServeMux) *http.ServeMux {
db := database.GetDB("neuralnexus")
rdb := database.GetRedis()
acctStore := auth.NewAccountStore(db)
sessStore := auth.NewSessionStore(db, rdb)
sessStore := sess.NewSessionStore(db, rdb)
alstore := accountlinking.NewStore(db)

mux.HandleFunc("POST /api/v1/auth/login", LoginHandler(acctStore, sessStore))
Expand All @@ -30,7 +31,7 @@ func ApplyRoutes(mux *http.ServeMux) *http.ServeMux {
}

// LoginHandler handles the login route
func LoginHandler(as auth.AccountStore, ss auth.SessionStore) http.HandlerFunc {
func LoginHandler(as auth.AccountStore, ss sess.SessionStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var login struct {
Username string `json:"username" xml:"username" validate:"required_without=Email"`
Expand Down Expand Up @@ -67,17 +68,17 @@ func LoginHandler(as auth.AccountStore, ss auth.SessionStore) http.HandlerFunc {
}

// LogoutHandler handles the logout route
func LogoutHandler(ss auth.SessionStore) http.HandlerFunc {
func LogoutHandler(ss sess.SessionStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(mw.SessionKey).(*auth.Session)
session := r.Context().Value(mw.SessionKey).(*sess.Session)
ss.DeleteSessionFromCache(session.ID)
responses.SendAndEncodeStruct(w, r, http.StatusOK, session)
ss.DeleteSessionInDB(session.ID)
}
}

// OAuthHandler handles the Discord OAuth route
func OAuthHandler(as auth.AccountStore, ss auth.SessionStore, las accountlinking.LinkAccountStore) http.HandlerFunc {
func OAuthHandler(as auth.AccountStore, ss sess.SessionStore, las accountlinking.LinkAccountStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
if code == "" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package auth
package sess

import "github.com/google/uuid"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package auth
package sess

import (
"context"
Expand Down
36 changes: 36 additions & 0 deletions modules/auth/session/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package sess

import (
"time"

perms "github.com/NeuralNexusDev/neuralnexus-api/modules/auth/permissions"
"github.com/google/uuid"
)

// Session struct
type Session struct {
ID uuid.UUID `json:"session_id" xml:"session_id" db:"session_id"`
UserID uuid.UUID `json:"user_id" xml:"user_id" db:"user_id"`
Permissions []string `json:"permissions" xml:"permissions" db:"permissions"`
IssuedAt int64 `json:"iat" xml:"iat" db:"iat"`
LastUsedAt int64 `json:"lua" xml:"lua" db:"lua"`
ExpiresAt int64 `json:"exp" xml:"exp" db:"exp"`
}

// HasPermission checks if a session has a permission
func (s *Session) HasPermission(permission perms.Scope) bool {
for _, p := range s.Permissions {
if p == permission.Name+"|"+permission.Value {
return true
}
}
return false
}

// IsExpired checks if a session is expired
func (s *Session) IsValid() bool {
if s.ExpiresAt == 0 {
return true
}
return time.Now().Unix() < s.ExpiresAt
}
File renamed without changes.
36 changes: 5 additions & 31 deletions modules/auth/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log"
"time"

perms "github.com/NeuralNexusDev/neuralnexus-api/modules/auth/permissions"
sess "github.com/NeuralNexusDev/neuralnexus-api/modules/auth/session"
"github.com/google/uuid"
"golang.org/x/crypto/argon2"
)
Expand Down Expand Up @@ -80,21 +82,11 @@ func (user *Account) RemoveRole(role string) {
}
}

// Session struct
type Session struct {
ID uuid.UUID `json:"session_id" xml:"session_id" db:"session_id"`
UserID uuid.UUID `json:"user_id" xml:"user_id" db:"user_id"`
Permissions []string `json:"permissions" xml:"permissions" db:"permissions"`
IssuedAt int64 `json:"iat" xml:"iat" db:"iat"`
LastUsedAt int64 `json:"lua" xml:"lua" db:"lua"`
ExpiresAt int64 `json:"exp" xml:"exp" db:"exp"`
}

// NewSession creates a new session
func (a *Account) NewSession(expiresAt int64) *Session {
func (a *Account) NewSession(expiresAt int64) *sess.Session {
permissions := []string{}
for _, r := range a.Roles {
role, err := GetRoleByName(r)
role, err := perms.GetRoleByName(r)
if err != nil {
log.Println(err)
continue
Expand All @@ -104,7 +96,7 @@ func (a *Account) NewSession(expiresAt int64) *Session {
}
}

return &Session{
return &sess.Session{
ID: uuid.New(),
UserID: a.UserID,
Permissions: permissions,
Expand All @@ -113,21 +105,3 @@ func (a *Account) NewSession(expiresAt int64) *Session {
ExpiresAt: expiresAt,
}
}

// HasPermission checks if a session has a permission
func (s *Session) HasPermission(permission Scope) bool {
for _, p := range s.Permissions {
if p == permission.Name+"|"+permission.Value {
return true
}
}
return false
}

// IsExpired checks if a session is expired
func (s *Session) IsValid() bool {
if s.ExpiresAt == 0 {
return true
}
return time.Now().Unix() < s.ExpiresAt
}
23 changes: 12 additions & 11 deletions modules/bee_name_generator/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"strconv"

mw "github.com/NeuralNexusDev/neuralnexus-api/middleware"
"github.com/NeuralNexusDev/neuralnexus-api/modules/auth"
perms "github.com/NeuralNexusDev/neuralnexus-api/modules/auth/permissions"
sess "github.com/NeuralNexusDev/neuralnexus-api/modules/auth/session"
"github.com/NeuralNexusDev/neuralnexus-api/modules/database"
"github.com/NeuralNexusDev/neuralnexus-api/responses"
)
Expand Down Expand Up @@ -41,8 +42,8 @@ func GetBeeNameHandler(s BNGStore) http.HandlerFunc {
// UploadBeeNameHandler Upload a bee name
func UploadBeeNameHandler(s BNGStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(mw.SessionKey).(*auth.Session)
if !session.HasPermission(auth.ScopeAdminBeeNameGenerator) {
session := r.Context().Value(mw.SessionKey).(*sess.Session)
if !session.HasPermission(perms.ScopeAdminBeeNameGenerator) {
responses.SendAndEncodeForbidden(w, r, "You do not have permission to upload bee names")
return
}
Expand All @@ -66,8 +67,8 @@ func UploadBeeNameHandler(s BNGStore) http.HandlerFunc {
// DeleteBeeName Delete a bee name
func DeleteBeeNameHandler(s BNGStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(mw.SessionKey).(*auth.Session)
if !session.HasPermission(auth.ScopeAdminBeeNameGenerator) {
session := r.Context().Value(mw.SessionKey).(*sess.Session)
if !session.HasPermission(perms.ScopeAdminBeeNameGenerator) {
responses.SendAndEncodeForbidden(w, r, "You do not have permission to delete bee names")
return
}
Expand Down Expand Up @@ -110,8 +111,8 @@ func SubmitBeeNameHandler(s BNGStore) http.HandlerFunc {
// GetBeeNameSuggestions Get a list of bee name suggestions
func GetBeeNameSuggestionsHandler(s BNGStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(mw.SessionKey).(*auth.Session)
if !session.HasPermission(auth.ScopeAdminBeeNameGenerator) {
session := r.Context().Value(mw.SessionKey).(*sess.Session)
if !session.HasPermission(perms.ScopeAdminBeeNameGenerator) {
responses.SendAndEncodeForbidden(w, r, "You do not have permission to get bee name suggestions")
return
}
Expand Down Expand Up @@ -143,8 +144,8 @@ func GetBeeNameSuggestionsHandler(s BNGStore) http.HandlerFunc {
// AcceptBeeNameSuggestionHandler Accept a bee name suggestion
func AcceptBeeNameSuggestionHandler(s BNGStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(mw.SessionKey).(*auth.Session)
if !session.HasPermission(auth.ScopeAdminBeeNameGenerator) {
session := r.Context().Value(mw.SessionKey).(*sess.Session)
if !session.HasPermission(perms.ScopeAdminBeeNameGenerator) {
responses.SendAndEncodeForbidden(w, r, "You do not have permission to accept bee name suggestions")
return
}
Expand All @@ -168,8 +169,8 @@ func AcceptBeeNameSuggestionHandler(s BNGStore) http.HandlerFunc {
// RejectBeeNameSuggestionHandler Reject a bee name suggestion
func RejectBeeNameSuggestionHandler(s BNGStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(mw.SessionKey).(*auth.Session)
if !session.HasPermission(auth.ScopeAdminBeeNameGenerator) {
session := r.Context().Value(mw.SessionKey).(*sess.Session)
if !session.HasPermission(perms.ScopeAdminBeeNameGenerator) {
responses.SendAndEncodeForbidden(w, r, "You do not have permission to reject bee name suggestions")
return
}
Expand Down
19 changes: 10 additions & 9 deletions modules/pet_pictures/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"strconv"

mw "github.com/NeuralNexusDev/neuralnexus-api/middleware"
"github.com/NeuralNexusDev/neuralnexus-api/modules/auth"
perms "github.com/NeuralNexusDev/neuralnexus-api/modules/auth/permissions"
sess "github.com/NeuralNexusDev/neuralnexus-api/modules/auth/session"
"github.com/NeuralNexusDev/neuralnexus-api/modules/database"
"github.com/NeuralNexusDev/neuralnexus-api/responses"
)
Expand All @@ -32,8 +33,8 @@ func ApplyRoutes(router *http.ServeMux) *http.ServeMux {
// CreatePetHandler - Create a new pet
func CreatePetHandler(s PetPicService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(mw.SessionKey).(auth.Session)
if !session.HasPermission(auth.ScopeAdminPetPictures) {
session := r.Context().Value(mw.SessionKey).(sess.Session)
if !session.HasPermission(perms.ScopeAdminPetPictures) {
responses.SendAndEncodeForbidden(w, r, "You do not have permission to create a pet")
return
}
Expand Down Expand Up @@ -105,8 +106,8 @@ func UpdatePetHandler(s PetPicService) http.HandlerFunc {
return
}

session := r.Context().Value(mw.SessionKey).(auth.Session)
if !session.HasPermission(auth.ScopePetPictures(pet.Name)) {
session := r.Context().Value(mw.SessionKey).(sess.Session)
if !session.HasPermission(perms.ScopePetPictures(pet.Name)) {
responses.SendAndEncodeForbidden(w, r, "You do not have permission to update this pet")
return
}
Expand Down Expand Up @@ -190,8 +191,8 @@ func UpdatePetPictureHandler(s PetPicService) http.HandlerFunc {
return
}

session := r.Context().Value(mw.SessionKey).(auth.Session)
if !session.HasPermission(auth.ScopePetPictures(pet.Name)) {
session := r.Context().Value(mw.SessionKey).(sess.Session)
if !session.HasPermission(perms.ScopePetPictures(pet.Name)) {
responses.SendAndEncodeForbidden(w, r, "You do not have permission to update this pet")
return
}
Expand Down Expand Up @@ -236,8 +237,8 @@ func DeletePetPictureHandler(s PetPicService) http.HandlerFunc {
return
}

session := r.Context().Value(mw.SessionKey).(auth.Session)
if !session.HasPermission(auth.ScopePetPictures(pet.Name)) {
session := r.Context().Value(mw.SessionKey).(sess.Session)
if !session.HasPermission(perms.ScopePetPictures(pet.Name)) {
responses.SendAndEncodeForbidden(w, r, "You do not have permission to update this pet")
return
}
Expand Down

0 comments on commit 13843da

Please sign in to comment.