Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20.2-alpine
FROM golang:1.23-alpine

WORKDIR /usr/app

Expand Down
15 changes: 10 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ GOIMPORTS := $(GOPATH_BIN)/goimports
GO_PACKAGES = $(shell go list ./... | grep -v vendor)
PACKAGE_BASE := github.com/sdslabs/nymeria

DB_HOST = $(shell awk '/db:/,/db_name:/' config.yaml | grep 'host:' | sed -n 's/.*host: *"\?\([^"]*\)"\?/\1/p')
DB_PORT = $(shell awk '/db:/,/db_name:/' config.yaml | grep 'port:' | sed -n 's/.*port: *"\?\([^"]*\)"\?/\1/p')
DB_USER = $(shell awk '/db:/,/db_name:/' config.yaml | grep 'user:' | sed -n 's/.*user: *"\?\([^"]*\)"\?/\1/p')
DB_PASS = $(shell awk '/db:/,/db_name:/' config.yaml | grep 'password:' | sed -n 's/.*password: *"\?\([^"]*\)"\?/\1/p')
DB_NAME = $(shell awk '/db:/,/db_name:/' config.yaml | grep 'db_name:' | sed -n 's/.*db_name: *"\?\([^"]*\)"\?/\1/p')
DB_HOST = $(shell grep -A6 "^db:" config.yaml | grep "host:" | head -1 | cut -d'"' -f2)
DB_PORT = $(shell grep -A6 "^db:" config.yaml | grep "port:" | head -1 | awk '{print $$2}')
DB_USER = $(shell grep -A6 "^db:" config.yaml | grep "user:" | head -1 | cut -d'"' -f2)
DB_PASS = $(shell grep -A6 "^db:" config.yaml | grep "password:" | head -1 | cut -d'"' -f2)
DB_NAME = $(shell grep -A6 "^db:" config.yaml | grep "db_name:" | head -1 | cut -d'"' -f2)

UP_MIGRATION_FILE = db/migrations/000001_init_schema.up.sql
DOWN_MIGRATION_FILE = db/migrations/000001_init_schema.down.sql
Expand Down Expand Up @@ -84,6 +84,11 @@ install-air:

apply-migration:
@echo "Applying migration..."
@echo "DB_HOST: $(DB_HOST)"
@echo "DB_PORT: $(DB_PORT)"
@echo "DB_USER: $(DB_USER)"
@echo "DB_PASS: $(DB_PASS)"
@echo "DB_NAME: $(DB_NAME)"
PGPASSWORD=$(DB_PASS) psql -h $(DB_HOST) -p $(DB_PORT) -U $(DB_USER) -d $(DB_NAME) -f $(UP_MIGRATION_FILE)

rollback-migration:
Expand Down
47 changes: 41 additions & 6 deletions api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func HandleGetApplication(c *gin.Context) {

}

func HandlePostApplication(c *gin.Context) {
func HandleCreateApplication(c *gin.Context) {
var body ApplicationPostBody
err := c.BindJSON(&body)

Expand Down Expand Up @@ -61,12 +61,12 @@ func HandlePostApplication(c *gin.Context) {
}

c.JSON(http.StatusOK, gin.H{
"status": "application created",
"message": "application created",
})

}

func HandlePutApplication(c *gin.Context) {
func HandleUpdateApplication(c *gin.Context) {
var body ApplicationPutBody
err := c.BindJSON(&body)

Expand Down Expand Up @@ -96,7 +96,7 @@ func HandlePutApplication(c *gin.Context) {
}

c.JSON(http.StatusOK, gin.H{
"status": "application updated",
"message": "application updated",
})

}
Expand Down Expand Up @@ -151,7 +151,7 @@ func HandleUpdateClientSecret(c *gin.Context) {
return
}

err = db.UpdateClientSecret(body.ID)
newSecret, err := db.UpdateClientSecret(body.ID)

if err != nil {
log.ErrorLogger("Client Secret update failed", err)
Expand All @@ -165,7 +165,42 @@ func HandleUpdateClientSecret(c *gin.Context) {
}

c.JSON(http.StatusOK, gin.H{
"message": "Client Secret updated successfully",
"message": "Client Secret updated successfully",
"newSecret": newSecret,
})

}

func HandleUpdateClientKey(c *gin.Context) {
var body ApplicationBody
err := c.BindJSON(&body)

if err != nil {
log.ErrorLogger("Unable to process json body", err)

errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": strings.Split(err.Error(), " ")[1],
"message": "Unable to process json body",
})
return
}

newKey, err := db.UpdateClientKey(body.ID)

if err != nil {
log.ErrorLogger("Client Key update failed", err)

errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": strings.Split(err.Error(), " ")[1],
"message": "Client Key update failed",
})
return
}

c.JSON(http.StatusOK, gin.H{
"message": "Client Key updated successfully",
"newClientKey": newKey,
})
}
52 changes: 28 additions & 24 deletions api/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"net/http"
"time"

"github.com/gin-contrib/cors"
Expand All @@ -14,7 +13,7 @@ func Start() {
r := gin.Default()
// Set up CORS middleware
config := cors.Config{
AllowOrigins: []string{"https://*.sdslabs.co"},
AllowOrigins: []string{"http://localhost:3000"}, // TODO: Change to production domain
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Authorization", "Content-Type"},
ExposeHeaders: []string{"Content-Length"},
Expand All @@ -26,7 +25,7 @@ func Start() {

// r.Use(k.Session())

r.GET("/ping", middleware.OnlyAdmin, func(c *gin.Context) {
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
Expand All @@ -36,48 +35,53 @@ func Start() {
r.GET("/mfa", HandleGetMFAFlow)
r.POST("/mfa", HandlePostMFAFlow)

r.POST("/create-identity", middleware.OnlyAdmin, HandleCreateIdentityFlow)
r.GET("/get-identity", middleware.OnlyAdmin, HandleGetIdentityFlow)
r.POST("/delete-identity", middleware.OnlyAdmin, HandleDeleteIdentityFlow)
r.GET("/list-identity", middleware.OnlyAdmin, HandleListIdentity)
r.PUT("/update-identity/ban", middleware.OnlyAdmin, HandleBanIdentity)
r.PUT("/update-identity/remove-ban", middleware.OnlyAdmin, HandleRemoveBanIdentity)
r.PUT("/update-identity/switch-roles", middleware.OnlyAdmin, HandleRoleSwitch)

r.GET("/register", HandleGetRegistrationFlow)
r.POST("/register", HandlePostRegistrationFlow)

r.GET("/logout", HandleGetLogoutFlow)
r.POST("/logout", HandlePostLogoutFlow)

r.GET("/status", HandleStatus)

r.GET("/recovery", HandleGetRecoveryFlow)
r.POST("/recovery", HandlePostRecoveryFlow)
r.POST("/recovery-code", HandlePostRecoveryCodeFlow)

r.GET("/settings", HandleGetSettingsFlow)
r.POST("/update-profile", HandleUpdateProfile)
r.PATCH("/update-profile", HandleUpdateProfile)
r.POST("/change-password", HandleChangePassword)
r.POST("/toggle-totp", HandleToggleTOTP)

r.GET("/verification", HandleGetVerificationFlow)
r.POST("/verification", HandlePostVerificationFlow)
r.POST("/verification-code", HandlePostVerificationCodeFlow)

r.POST("/get_profile", HandlePostProfile)
r.POST("/get_verified_status", HandleGetVerifiedStatus)
r.POST("/verify_app", middleware.HandleAppAuthorization, func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "Authorized",
})
})
r.GET("/get-profile", HandlePostProfile)

// Verify User Session
r.GET("/verify-session", HandleVerifySession)

// Application Authorization
r.POST("/verify-app", HandleAppAuthorization)

// Admin Routes
r.Use(middleware.OnlyAdmin)

// Identity Management
r.POST("/create-identity", HandleCreateIdentityFlow)
r.GET("/get-identity", HandleGetIdentityFlow)
r.POST("/delete-identity", HandleDeleteIdentityFlow)
r.GET("/list-identity", HandleListIdentity)
r.PUT("/update-identity/ban", HandleBanIdentity)
r.PUT("/update-identity/remove-ban", HandleRemoveBanIdentity)
r.PUT("/update-identity/switch-roles", HandleRoleSwitch)

// Application Management
r.GET("/application", HandleGetApplication)
r.POST("/application", HandlePostApplication)
r.PUT("/application", HandlePutApplication)
r.POST("/application", HandleCreateApplication)
r.PUT("/application", HandleUpdateApplication)
r.DELETE("/application", HandleDeleteApplication)
r.PATCH("/update-client-secret", HandleUpdateClientSecret)
r.PATCH("/update-client-key", HandleUpdateClientKey)

r.POST("/update-client-secret", HandleUpdateClientSecret)
r.Run(":9898")
// listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
28 changes: 8 additions & 20 deletions api/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,13 @@ func HandlePostProfile(c *gin.Context) {
}
identity := session.GetIdentity()
traits := identity.GetTraits()
profile := traits.(map[string]interface{})

c.JSON(http.StatusOK, profile)
}

func HandleGetVerifiedStatus(c *gin.Context) {
session, err := middleware.GetSession(c)
if err != nil {
log.ErrorLogger("Unable to get session", err)
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to get session",
})
return
}
identity := session.GetIdentity()
verifiableAddresses := identity.GetVerifiableAddresses()
emails := verifiableAddresses

c.JSON(http.StatusOK, emails)
c.JSON(http.StatusOK, gin.H{
"message": "Profile fetched successfully",
"profile": gin.H{
"identityId": identity.GetId(),
"status": identity.GetState(),
"traits": traits,
},
})
}
9 changes: 9 additions & 0 deletions api/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ func HandlePostRegistrationFlow(c *gin.Context) {
return
}

err = helper.VerifyFields(t.Traits.Email, t.Traits.Username, t.Traits.Name, t.Traits.PhoneNumber, t.Password)

if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
"message": "Missing required fields",
})
return
}
flowID, sessionCookies, errMsg, err := registration.SubmitRegistrationFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Password, t.Traits)

if err != nil {
Expand Down
Loading