This repository has been archived by the owner on Dec 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from p-society/auth/rbac
feat/fix: adds rbac
- Loading branch information
Showing
12 changed files
with
173 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt" | ||
"github.com/p-society/gc-server/auth/internal/db" | ||
"github.com/p-society/gc-server/auth/pkg/security" | ||
model "github.com/p-society/gc-server/schemas/pkg/models" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
func Login(w http.ResponseWriter, r *http.Request) { | ||
var ( | ||
p model.Player | ||
reqBody struct { | ||
Mail string `json:"mail"` | ||
Password string `json:"password"` | ||
} | ||
) | ||
w.Header().Set("Content-Type", "application/json") | ||
|
||
if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil { | ||
json.NewEncoder(w).Encode(map[string]interface{}{ | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
filter := bson.M{ | ||
"email": reqBody.Mail, | ||
} | ||
|
||
err := db.PlayerCollection.FindOne(context.TODO(), filter).Decode(&p) | ||
if err == mongo.ErrNoDocuments { | ||
json.NewEncoder(w).Encode(map[string]interface{}{ | ||
"error": err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
err = bcrypt.CompareHashAndPassword([]byte(p.Password), []byte(reqBody.Password)) | ||
if err != nil { | ||
json.NewEncoder(w).Encode(map[string]interface{}{ | ||
"error": err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
p.StandardClaims = jwt.StandardClaims{ | ||
IssuedAt: time.Now().Unix(), | ||
ExpiresAt: time.Now().Add(time.Minute * 60 * 24 * 12).Unix(), | ||
} | ||
|
||
token, err := security.NewAccessToken(p) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
json.NewEncoder(w).Encode(map[string]interface{}{ | ||
"accessToken": token, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func ExtractTokenFromHeader(r *http.Request) (string, error) { | ||
|
||
authHeader := r.Header.Get("Authorization") | ||
if authHeader == "" && strings.Split(authHeader, " ")[0] == "Bearer" { | ||
return "", fmt.Errorf("token not found") | ||
} | ||
|
||
return strings.Split(authHeader, " ")[1], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package internal | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
|
2 changes: 1 addition & 1 deletion
2
auth/internal/unique_entries.go → auth/internal/utils/unique_entries.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package internal | ||
package utils | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package security | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/p-society/gc-server/auth/internal/utils" | ||
) | ||
|
||
// RoleGuard is a middleware for role-based access control | ||
type RoleGuard struct { | ||
AllowedRoles []string | ||
Handler http.Handler | ||
} | ||
|
||
// ServeHTTP implements the http.Handler interface for RoleGuard | ||
func (rg *RoleGuard) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
var ( | ||
token string | ||
err error | ||
) | ||
|
||
token, err = utils.ExtractTokenFromHeader(r) | ||
if err != nil { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
p := ParseAccessToken(token) | ||
|
||
// Check if user's role is allowed | ||
if !contains(rg.AllowedRoles, p.Role) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusForbidden) | ||
return | ||
} | ||
|
||
// Call the next handler in the chain | ||
rg.Handler.ServeHTTP(w, r) | ||
} | ||
|
||
// contains checks if a string is present in a slice of strings | ||
func contains(roles []string, role string) bool { | ||
for _, r := range roles { | ||
if r == role { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters