Skip to content

Commit

Permalink
Merge pull request #19 from p-society/main
Browse files Browse the repository at this point in the history
Registration Service Sync
  • Loading branch information
zakhaev26 authored Jan 2, 2024
2 parents 0e9289f + c051f18 commit c13b829
Show file tree
Hide file tree
Showing 20 changed files with 777 additions and 12 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/lint-go-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Go Linter CI

on:
push:
branches:
- api/basketball
- main
jobs:
lint:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.21.4

- name: Install golint
run: |
cd src/api/basketball
go mod tidy
go get -u golang.org/x/lint/golint
- name: Run golint
run: golint
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ secrets
# Dependency directories (remove the comment below to include it)
vendor/
src/api/basketball/.env

services/mailing/.env
connector/.env

# Go workspace file
go.work
218 changes: 218 additions & 0 deletions connector/controllers/playerRegistration.controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package playerRegistation

import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"strconv"
"sync"
"time"

generaldb "github.com/p-society/gCSB/connector/db"
mailer "github.com/p-society/gCSB/connector/helpers"
tempModel "github.com/p-society/gCSB/connector/model"
"go.mongodb.org/mongo-driver/bson"
)

const otpValidityMinutes = 5

func generateOTP() (int, time.Time) {
rand.Seed(time.Now().UnixNano())

randomNumber := rand.Intn(10000) + 10000
expirationTime := time.Now().Add(otpValidityMinutes * time.Minute)

return randomNumber, expirationTime
}

func isOTPValid(expirationTime time.Time) bool {
return time.Now().Before(expirationTime)
}

func PlayerRegistrationController(w http.ResponseWriter, r *http.Request) {
var player tempModel.PlayerTemp
err := json.NewDecoder(r.Body).Decode(&player)
if err != nil {
fmt.Println(err)
return
}

OTP, expirationTime := generateOTP()
player.OTP = OTP
player.Verified = false
player.OTPExpiration = expirationTime
inserted, err := generaldb.Collection.InsertOne(r.Context(), player)

content := `
<html>
<body style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #f5f5f5; color: #333; text-align: center; padding: 20px;">
<img src="https://avatars.githubusercontent.com/u/29895434?s=400&u=59686b8af9d2476daa8886e6616fed4c76588596&v=4" alt="GCSB Logo" style="max-width: 200px; margin-bottom: 20px;">
<h2 style="color: #007BFF;">Dear ` + player.Name + `,</h2>
<p style="font-size: 16px;">Greetings from PSoc, IIIT-Bhubaneswar!</p>
<div style="background-color: #ffffff; border-radius: 10px; padding: 20px; box-shadow: 0 0 10px rgba(0,0,0,0.1);">
<p style="font-size: 18px;">Your One-Time Password (OTP) for GCSB Verification:</p>
<h3 style="background-color: #007BFF; color: #fff; padding: 10px; border-radius: 5px; font-size: 24px;">` + strconv.Itoa(OTP) + `</h3>
<p style="font-size: 16px;">This OTP is valid for a limited time to ensure the security of your account.</p>
</div>
<p style="font-size: 16px; margin-top: 20px;">If you did not request this OTP, please disregard this email.</p>
<p style="font-size: 16px;">Best regards,<br/>
The PSoc Team, IIIT-Bhubaneswar</p>
</body>
</html>`

subject := "Player Email Verification - GCSB"

var wg sync.WaitGroup

ch := make(chan string)
wg.Add(1)

go func() {
defer wg.Done()
val := mailer.SendMail(content, subject, player.Email)
ch <- val
close(ch)
}()

fmt.Println("val:", <-ch)
wg.Wait()

if err != nil {
fmt.Println(err)
return
}

jsonPayload := map[string]interface{}{
"inserted": inserted,
"message": "Verify yourself by plugging the OTP sent on you email provided.",
}

err = json.NewEncoder(w).Encode(jsonPayload)

if err != nil {
fmt.Println(err)
return
}

}

type CallbackMessage struct {
Name string `json:"name,omitempty" bson:"name,omitempty"`
ReceivedOTP int `json:"otp"`
}

func VerifyPlayerController(w http.ResponseWriter, r *http.Request) {

var (
message CallbackMessage
player tempModel.PlayerTemp
)

err := json.NewDecoder(r.Body).Decode(&message)

if err != nil {
fmt.Println(err)
return
}

filter := bson.M{
"name": message.Name,
}

res := generaldb.Collection.FindOne(r.Context(), filter)
res.Decode(&player)

if isOTPValid(player.OTPExpiration) && player.OTP == message.ReceivedOTP {

filter := bson.M{"name": player.Name}

update := bson.D{
{"$set", bson.D{
{"verified", true},
{"otp", ""},
{"otpexpirationtime", "infinite"},
}},
}

fmt.Println("User is Certified Stoner")
updres, err := generaldb.Collection.UpdateOne(r.Context(), filter, update)

if err != nil {
fmt.Println("err:", err)
json.NewEncoder(w).Encode(err)
}
fmt.Println(updres)

w.Write([]byte("Verified!"))
} else {
json.NewEncoder(w).Encode(map[string]interface{}{
"err": "OTP validation failed,Either Timed Out or Invalid OTP!",
})
}

}

const CAPTAIN_UNIQUE_KEY = "iiitbbsr"

/*
Captain is manually added to the <sport>:<collection> by default.
Work of Captain is to select the players and create the roster.
*/

type CaptainResponseCreator struct {
Team string `json:"team,omitempty"`
Sport string `json:"sport,omitempty"`
}

func CaptainFetchController(w http.ResponseWriter, r *http.Request) {

var capFilter CaptainResponseCreator
var players []tempModel.PlayerTemp
err := json.NewDecoder(r.Body).Decode(&capFilter)

if err != nil {
json.NewEncoder(w).Encode(err)
fmt.Println(err)
return
}

filter := bson.M{}

cursor, err := generaldb.Collection.Find(r.Context(), filter)

if err != nil {
json.NewEncoder(w).Encode(err)
fmt.Println(err)
return
}

defer cursor.Close(r.Context())

for cursor.Next(r.Context()) {
var player tempModel.PlayerTemp
err := cursor.Decode(&player)
if err != nil {
log.Fatal(err)
}
players = append(players, player)
}

// Checking for errors during cursor iteration
if err := cursor.Err(); err != nil {
log.Fatal(err)
}

// Print or process the retrieved documents
for _, doc := range players {
fmt.Printf("%+v\n", doc)
}
}
42 changes: 42 additions & 0 deletions connector/db/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package generaldb

import (
"context"
"fmt"
"log"
"os"

"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

var Collection *mongo.Collection
var Database *mongo.Database

func Init() {
err := godotenv.Load()

if err != nil {
log.Fatal("Error loading .env file")
}
fmt.Println("Env Loaded Successfully.")

// Access environment variables
dbLink := os.Getenv("MONGO_URI")
const databaseName = "generaldb"
const collection1Name = "all-players"

clientOptions := options.Client().ApplyURI(dbLink)
client, err := mongo.Connect(context.TODO(), clientOptions)

if err != nil {
log.Fatal("Error Occurred while connecting to the database:", err)
}

fmt.Println("Connection to Database Successful")

Database = client.Database(databaseName)
Collection = Database.Collection(collection1Name)
fmt.Printf("Collection Instance %s is Ready.", collection1Name)
}
22 changes: 22 additions & 0 deletions connector/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module github.com/p-society/gCSB/connector

go 1.21.4

require (
github.com/gorilla/mux v1.8.1
github.com/joho/godotenv v1.5.1
go.mongodb.org/mongo-driver v1.13.1
)

require (
github.com/golang/snappy v0.0.4 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
68 changes: 68 additions & 0 deletions connector/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk=
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk=
go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Loading

0 comments on commit c13b829

Please sign in to comment.