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.
- Loading branch information
Showing
8 changed files
with
186 additions
and
48 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,69 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/p-society/gc-server/auth/internal" | ||
"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" | ||
) | ||
|
||
func CallbackVerification(w http.ResponseWriter, r *http.Request) { | ||
|
||
var ( | ||
p model.Player | ||
pv *model.ValidationSchema | ||
reqBody struct { | ||
OTP int `json:"otp"` | ||
} | ||
) | ||
|
||
json.NewDecoder(r.Body).Decode(&reqBody) | ||
authHeader := r.Header.Get("Authorization") | ||
|
||
if authHeader == "" && strings.Split(authHeader, " ")[0] != "Bearer" { | ||
r.Header.Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(map[string]interface{}{ | ||
"error": "Authorization Token Not found", | ||
}) | ||
return | ||
} | ||
|
||
token := strings.Split(authHeader, " ")[1] | ||
pv = security.ParseAccessToken(token) | ||
|
||
if err := pv.Valid(); err != nil { | ||
r.Header.Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(map[string]interface{}{ | ||
"error": err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
if err := internal.CheckOTP(pv.OTP, reqBody.OTP); err != nil { | ||
r.Header.Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(map[string]interface{}{ | ||
"error": err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
p = model.Player{ | ||
FirstName: pv.FirstName, | ||
LastName: pv.LastName, | ||
Role: pv.Role, | ||
Email: pv.Email, | ||
Branch: pv.Branch, | ||
Year: pv.Branch, | ||
ContactNo: pv.ContactNo, | ||
Social: pv.Social, | ||
} | ||
|
||
res, _ := db.PlayerCollection.InsertOne(context.TODO(), p) | ||
|
||
json.NewEncoder(w).Encode(res.InsertedID) | ||
} |
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,21 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
) | ||
|
||
func GenerateOTP(len int) int { | ||
prng := rand.Float64() | ||
prng *= 100000 | ||
return int(prng) | ||
} | ||
|
||
func CheckOTP(actualOTP int, sentOTP int) error { | ||
|
||
if actualOTP == sentOTP { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("OTP Invalid") | ||
} |
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,31 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/p-society/gc-server/auth/internal/db" | ||
model "github.com/p-society/gc-server/schemas/pkg/models" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func IsUniqueInDB(email string) error { | ||
fmt.Println("db searching ..", email) | ||
filter := bson.M{ | ||
"email": email, | ||
} | ||
|
||
var player model.Player | ||
err := db.PlayerCollection.FindOne(context.TODO(), filter).Decode(&player) | ||
if err != nil { | ||
if errors.Is(err, mongo.ErrNoDocuments) { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
fmt.Println("player", player) | ||
return fmt.Errorf("player already exists") | ||
} |
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,43 +1,19 @@ | ||
package security | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/golang-jwt/jwt" | ||
model "github.com/p-society/gc-server/schemas/pkg/models" | ||
) | ||
|
||
var _ jwt.Claims = model.Player{} | ||
|
||
func Tokenize(p *model.Player) (string, error) { | ||
signingKey := []byte("saswat,tu23kranklekeiiitkyunaaya") | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, p) | ||
|
||
tokenString, err := token.SignedString(signingKey) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
func NewAccessToken(claims model.ValidationSchema) (string, error) { | ||
accessToken := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
|
||
return tokenString, nil | ||
return accessToken.SignedString([]byte("saswat,tu23kranklekeiiitkyunaaya")) | ||
} | ||
|
||
func DecodeToken(tokenString string) (model.Player, error) { | ||
signingKey := []byte("saswat,tu23kranklekeiiitkyunaaya") // Replace with actual secret key | ||
|
||
token, err := jwt.ParseWithClaims(tokenString, &model.Player{}, func(token *jwt.Token) (interface{}, error) { | ||
return signingKey, nil | ||
func ParseAccessToken(accessToken string) *model.ValidationSchema { | ||
parsedAccessToken, _ := jwt.ParseWithClaims(accessToken, &model.ValidationSchema{}, func(token *jwt.Token) (interface{}, error) { | ||
return []byte([]byte("saswat,tu23kranklekeiiitkyunaaya")), nil | ||
}) | ||
|
||
if err != nil { | ||
fmt.Println("err heer") | ||
return model.Player{}, err | ||
} | ||
|
||
if claims, ok := token.Claims.(*model.Player); ok && token.Valid { | ||
fmt.Println("fname = ",&claims.FirstName) | ||
return *claims, nil | ||
} | ||
|
||
return model.Player{}, fmt.Errorf("invalid token claims") | ||
return parsedAccessToken.Claims.(*model.ValidationSchema) | ||
} |
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