Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
760a778a27 | ||
|
|
7597e05243
|
||
|
|
636b0280c7 | ||
|
|
223b7919f9 | ||
|
|
66221e504c
|
||
|
|
093888f7fb
|
||
|
|
225dc0c963
|
||
|
|
c7b45952fe
|
||
|
|
f305e5b2e9
|
||
|
|
9f5029dd6e | ||
|
|
d01fae0775 |
@@ -5,3 +5,5 @@ DB_PASSWORD=password
|
|||||||
DB_HOST=auth_db
|
DB_HOST=auth_db
|
||||||
DB_PORT=5432
|
DB_PORT=5432
|
||||||
DB_SSLMODE=disable
|
DB_SSLMODE=disable
|
||||||
|
ENABLE_REGISTRATION=true
|
||||||
|
ALLOWED_ORIGINS="http://textsender.com"
|
||||||
|
|||||||
@@ -5,3 +5,5 @@ DB_PASSWORD=yEDjWZCH2vdctjn!
|
|||||||
DB_HOST=localhost
|
DB_HOST=localhost
|
||||||
DB_PORT=5432
|
DB_PORT=5432
|
||||||
DB_SSLMODE=disable
|
DB_SSLMODE=disable
|
||||||
|
ENABLE_REGISTRATION=true
|
||||||
|
ALLOWED_ORIGINS="http://textsender.com"
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ jobs:
|
|||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v4
|
uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: '1.25.3' # You can specify a specific version or 'stable'
|
go-version: '1.25.4' # You can specify a specific version or 'stable'
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
@@ -24,7 +24,7 @@ jobs:
|
|||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/textsender_models_deploy_key
|
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/textsender_models_deploy_key
|
||||||
chmod 600 ~/.ssh/textsender_models_deploy_key
|
chmod 600 ~/.ssh/textsender_models_deploy_key
|
||||||
ssh-keyscan ${{ vars.MY_HOST }} >> ~/.ssh/known_hosts
|
ssh-keyscan ${{ secrets.MY_HOST }} >> ~/.ssh/known_hosts
|
||||||
|
|
||||||
eval $(ssh-agent -s)
|
eval $(ssh-agent -s)
|
||||||
ssh-add -v ~/.ssh/textsender_models_deploy_key
|
ssh-add -v ~/.ssh/textsender_models_deploy_key
|
||||||
@@ -66,9 +66,9 @@ jobs:
|
|||||||
uses: actions/checkout@v5
|
uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Setup Go
|
- name: Setup Go
|
||||||
uses: actions/setup-go@v4
|
uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: '1.25.3'
|
go-version: '1.25.4'
|
||||||
|
|
||||||
- name: Install PostgreSQL client
|
- name: Install PostgreSQL client
|
||||||
run: sudo apt update && sudo apt-get install -y postgresql-client
|
run: sudo apt update && sudo apt-get install -y postgresql-client
|
||||||
@@ -99,19 +99,21 @@ jobs:
|
|||||||
echo "Parent directory"
|
echo "Parent directory"
|
||||||
echo `pwd`
|
echo `pwd`
|
||||||
|
|
||||||
echo "SECRET_KEY=$SECRET_KEY" >> .env
|
echo "SECRET_KEY=$SECRET_KEY" > .env
|
||||||
echo "DB_NAME=$DB_NAME" > .env
|
echo "DB_NAME=$DB_NAME" >> .env
|
||||||
echo "DB_USER=$DB_USER" >> .env
|
echo "DB_USER=$DB_USER" >> .env
|
||||||
echo "DB_PASSWORD=$DB_PASSWORD" >> .env
|
echo "DB_PASSWORD=$DB_PASSWORD" >> .env
|
||||||
echo "DB_HOST=$DB_HOST" >> .env
|
echo "DB_HOST=$DB_HOST" >> .env
|
||||||
echo "DB_PORT=$DB_PORT" >> .env
|
echo "DB_PORT=$DB_PORT" >> .env
|
||||||
echo "DB_SSLMODE=$DB_SSLMODE" >> .env
|
echo "DB_SSLMODE=$DB_SSLMODE" >> .env
|
||||||
|
echo "ENABLE_REGISTRATION=true" >> .env
|
||||||
|
echo "ALLOWED_ORIGINS=http://localhost:9080" >> .env
|
||||||
|
|
||||||
echo "Initializing config"
|
echo "Initializing config"
|
||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/textsender_models_deploy_key
|
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/textsender_models_deploy_key
|
||||||
chmod 600 ~/.ssh/textsender_models_deploy_key
|
chmod 600 ~/.ssh/textsender_models_deploy_key
|
||||||
ssh-keyscan ${{ vars.MY_HOST }} >> ~/.ssh/known_hosts
|
ssh-keyscan ${{ secrets.MY_HOST }} >> ~/.ssh/known_hosts
|
||||||
|
|
||||||
eval $(ssh-agent -s)
|
eval $(ssh-agent -s)
|
||||||
ssh-add -v ~/.ssh/textsender_models_deploy_key
|
ssh-add -v ~/.ssh/textsender_models_deploy_key
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
# Multi-stage Dockerfile for Go application
|
# Multi-stage Dockerfile for Go application
|
||||||
FROM golang:1.25.3 AS builder
|
FROM golang:1.25.4 AS builder
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
|||||||
+15
-6
@@ -12,6 +12,7 @@ import (
|
|||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/go-chi/chi/v5/middleware"
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
|
"github.com/go-chi/cors"
|
||||||
"github.com/swaggo/http-swagger/v2"
|
"github.com/swaggo/http-swagger/v2"
|
||||||
|
|
||||||
_ "git.kundeng.us/phoenix/textsender-auth/docs"
|
_ "git.kundeng.us/phoenix/textsender-auth/docs"
|
||||||
@@ -20,7 +21,6 @@ import (
|
|||||||
"git.kundeng.us/phoenix/textsender-auth/internal/handler"
|
"git.kundeng.us/phoenix/textsender-auth/internal/handler"
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/handler/endpoint"
|
"git.kundeng.us/phoenix/textsender-auth/internal/handler/endpoint"
|
||||||
mdleware "git.kundeng.us/phoenix/textsender-auth/internal/middleware"
|
mdleware "git.kundeng.us/phoenix/textsender-auth/internal/middleware"
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/model"
|
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/store"
|
"git.kundeng.us/phoenix/textsender-auth/internal/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -73,16 +73,25 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Services
|
// Services
|
||||||
userStore := model.NewUserStore(db.Pool)
|
userStore := store.NewUserStore(db.Pool)
|
||||||
serviceStore := store.NewServiceStore(db.Pool)
|
serviceStore := store.NewServiceStore(db.Pool)
|
||||||
|
|
||||||
userHandler := handler.NewUserHandler(userStore)
|
userHandler := handler.NewUserHandler(cfg, userStore)
|
||||||
loginHandler := handler.NewLoginHandler(userStore)
|
loginHandler := handler.NewLoginHandler(cfg, userStore)
|
||||||
serviceHandler := handler.NewServiceHandler(serviceStore)
|
serviceHandler := handler.NewServiceHandler(cfg, serviceStore)
|
||||||
refreshHandler := handler.NewRefreshHandler(userStore, serviceStore)
|
refreshHandler := handler.NewRefreshHandler(cfg, userStore, serviceStore)
|
||||||
|
|
||||||
router := chi.NewRouter()
|
router := chi.NewRouter()
|
||||||
|
|
||||||
|
// Configure CORS
|
||||||
|
router.Use(cors.Handler(cors.Options{
|
||||||
|
AllowedOrigins: cfg.AllowedOrigins,
|
||||||
|
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
|
||||||
|
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
||||||
|
ExposedHeaders: []string{"Link", "X-Total-Count"},
|
||||||
|
AllowCredentials: true,
|
||||||
|
MaxAge: 300, // 5 minutes
|
||||||
|
}))
|
||||||
router.Use(middleware.Logger)
|
router.Use(middleware.Logger)
|
||||||
router.Use(middleware.Recoverer)
|
router.Use(middleware.Recoverer)
|
||||||
router.Use(middleware.Timeout(60 * time.Second))
|
router.Use(middleware.Timeout(60 * time.Second))
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ import (
|
|||||||
"git.kundeng.us/phoenix/textsender-auth/internal/db"
|
"git.kundeng.us/phoenix/textsender-auth/internal/db"
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/handler"
|
"git.kundeng.us/phoenix/textsender-auth/internal/handler"
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/handler/endpoint"
|
"git.kundeng.us/phoenix/textsender-auth/internal/handler/endpoint"
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/model"
|
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/store"
|
"git.kundeng.us/phoenix/textsender-auth/internal/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,12 +37,12 @@ func TestMain(m *testing.M) {
|
|||||||
panic("Failed to initialize database")
|
panic("Failed to initialize database")
|
||||||
}
|
}
|
||||||
|
|
||||||
userStore := model.NewUserStore(database.Pool)
|
userStore := store.NewUserStore(database.Pool)
|
||||||
serviceStore := store.NewServiceStore(database.Pool)
|
serviceStore := store.NewServiceStore(database.Pool)
|
||||||
userHandler := handler.NewUserHandler(userStore)
|
userHandler := handler.NewUserHandler(cfg, userStore)
|
||||||
loginHandler := handler.NewLoginHandler(userStore)
|
loginHandler := handler.NewLoginHandler(cfg, userStore)
|
||||||
serviceHandler := handler.NewServiceHandler(serviceStore)
|
serviceHandler := handler.NewServiceHandler(cfg, serviceStore)
|
||||||
refreshHandler := handler.NewRefreshHandler(userStore, serviceStore)
|
refreshHandler := handler.NewRefreshHandler(cfg, userStore, serviceStore)
|
||||||
|
|
||||||
testRouter = mux.NewRouter()
|
testRouter = mux.NewRouter()
|
||||||
testRouter.HandleFunc(endpoint.Register, userHandler.Register).Methods("POST")
|
testRouter.HandleFunc(endpoint.Register, userHandler.Register).Methods("POST")
|
||||||
@@ -76,9 +75,10 @@ func load() *config.Config {
|
|||||||
dbConnString := unpackedConnString.Parse()
|
dbConnString := unpackedConnString.Parse()
|
||||||
|
|
||||||
return &config.Config{
|
return &config.Config{
|
||||||
DBConnString: dbConnString,
|
DBConnString: dbConnString,
|
||||||
ServerPort: *port,
|
ServerPort: *port,
|
||||||
ResetDB: *resetDb,
|
ResetDB: *resetDb,
|
||||||
|
EnableRegistration: config.CheckRegistration(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -108,6 +108,12 @@ const docTemplate = `{
|
|||||||
"$ref": "#/definitions/handler.RegisterResponse"
|
"$ref": "#/definitions/handler.RegisterResponse"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"403": {
|
||||||
|
"description": "Forbidden",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/handler.RegisterResponse"
|
||||||
|
}
|
||||||
|
},
|
||||||
"500": {
|
"500": {
|
||||||
"description": "Internal Server Error",
|
"description": "Internal Server Error",
|
||||||
"schema": {
|
"schema": {
|
||||||
@@ -204,6 +210,12 @@ const docTemplate = `{
|
|||||||
"$ref": "#/definitions/handler.ServiceCreationResponse"
|
"$ref": "#/definitions/handler.ServiceCreationResponse"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"403": {
|
||||||
|
"description": "Forbidden",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/handler.ServiceCreationResponse"
|
||||||
|
}
|
||||||
|
},
|
||||||
"500": {
|
"500": {
|
||||||
"description": "Internal Server Error",
|
"description": "Internal Server Error",
|
||||||
"schema": {
|
"schema": {
|
||||||
|
|||||||
@@ -102,6 +102,12 @@
|
|||||||
"$ref": "#/definitions/handler.RegisterResponse"
|
"$ref": "#/definitions/handler.RegisterResponse"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"403": {
|
||||||
|
"description": "Forbidden",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/handler.RegisterResponse"
|
||||||
|
}
|
||||||
|
},
|
||||||
"500": {
|
"500": {
|
||||||
"description": "Internal Server Error",
|
"description": "Internal Server Error",
|
||||||
"schema": {
|
"schema": {
|
||||||
@@ -198,6 +204,12 @@
|
|||||||
"$ref": "#/definitions/handler.ServiceCreationResponse"
|
"$ref": "#/definitions/handler.ServiceCreationResponse"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"403": {
|
||||||
|
"description": "Forbidden",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/handler.ServiceCreationResponse"
|
||||||
|
}
|
||||||
|
},
|
||||||
"500": {
|
"500": {
|
||||||
"description": "Internal Server Error",
|
"description": "Internal Server Error",
|
||||||
"schema": {
|
"schema": {
|
||||||
|
|||||||
@@ -171,6 +171,10 @@ paths:
|
|||||||
description: Bad Request
|
description: Bad Request
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/definitions/handler.RegisterResponse'
|
$ref: '#/definitions/handler.RegisterResponse'
|
||||||
|
"403":
|
||||||
|
description: Forbidden
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/handler.RegisterResponse'
|
||||||
"500":
|
"500":
|
||||||
description: Internal Server Error
|
description: Internal Server Error
|
||||||
schema:
|
schema:
|
||||||
@@ -231,6 +235,10 @@ paths:
|
|||||||
description: Bad Request
|
description: Bad Request
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/definitions/handler.ServiceCreationResponse'
|
$ref: '#/definitions/handler.ServiceCreationResponse'
|
||||||
|
"403":
|
||||||
|
description: Forbidden
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/handler.ServiceCreationResponse'
|
||||||
"500":
|
"500":
|
||||||
description: Internal Server Error
|
description: Internal Server Error
|
||||||
schema:
|
schema:
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
module git.kundeng.us/phoenix/textsender-auth
|
module git.kundeng.us/phoenix/textsender-auth
|
||||||
|
|
||||||
go 1.25.3
|
go 1.25.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.kundeng.us/phoenix/textsender-models v0.0.8-main-3ab81c2b6d-556
|
git.kundeng.us/phoenix/textsender-models v0.0.12
|
||||||
github.com/go-chi/chi/v5 v5.2.3
|
github.com/go-chi/chi/v5 v5.2.3
|
||||||
|
github.com/go-chi/cors v1.2.2
|
||||||
github.com/golang-jwt/jwt/v5 v5.3.0
|
github.com/golang-jwt/jwt/v5 v5.3.0
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/gorilla/mux v1.8.1
|
github.com/gorilla/mux v1.8.1
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
git.kundeng.us/phoenix/textsender-models v0.0.8-main-3ab81c2b6d-556 h1:iVE8Dg3aYugoZseneN9tQtdV2cymukspShF7dfExSVw=
|
git.kundeng.us/phoenix/textsender-models v0.0.12 h1:ps9H3FS5LyCwQhAiIvg4vYyfLZZ64dex1y9ytb0o9C4=
|
||||||
git.kundeng.us/phoenix/textsender-models v0.0.8-main-3ab81c2b6d-556/go.mod h1:lx5MCnOgGgsdpwzrfi9uph5xmkeb6H8AuexUNGss2no=
|
git.kundeng.us/phoenix/textsender-models v0.0.12/go.mod h1:9iPDQJg1Tc6WMNoW5+f8YKmnosMwlWHJ++hmxNLDEe0=
|
||||||
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
|
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
|
||||||
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
|
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
|
||||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
@@ -8,6 +8,8 @@ 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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
|
github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
|
||||||
github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
|
github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
|
||||||
|
github.com/go-chi/cors v1.2.2 h1:Jmey33TE+b+rB7fT8MUy1u0I4L+NARQlK6LhzKPSyQE=
|
||||||
|
github.com/go-chi/cors v1.2.2/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
|
||||||
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
|
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
|
||||||
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
|
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
|
||||||
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
|
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
|
||||||
@@ -13,9 +15,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
DBConnString string
|
DBConnString string
|
||||||
ServerPort string
|
ServerPort string
|
||||||
ResetDB bool
|
ResetDB bool
|
||||||
|
EnableRegistration bool
|
||||||
|
AllowedOrigins []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConnectionInfo struct {
|
type ConnectionInfo struct {
|
||||||
@@ -52,16 +56,35 @@ func Load() *Config {
|
|||||||
|
|
||||||
err := godotenv.Load()
|
err := godotenv.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error loading .env file")
|
cwd, _ := os.Getwd()
|
||||||
|
envPath := path.Join(cwd, "../..", ".env")
|
||||||
|
if err = godotenv.Load(envPath); err != nil {
|
||||||
|
prevPath := path.Join(envPath, "../..", ".env")
|
||||||
|
if err = godotenv.Load(prevPath); err != nil {
|
||||||
|
log.Fatal("Error loading .env file")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unpackedConnString := UnpackDBConnString()
|
unpackedConnString := UnpackDBConnString()
|
||||||
dbConnString := unpackedConnString.Parse()
|
dbConnString := unpackedConnString.Parse()
|
||||||
|
allowedOrigins := unpackAllowedOrigin()
|
||||||
|
|
||||||
return &Config{
|
if len(allowedOrigins) > 0 {
|
||||||
DBConnString: dbConnString,
|
return &Config{
|
||||||
ServerPort: *port,
|
DBConnString: dbConnString,
|
||||||
ResetDB: *resetDb,
|
ServerPort: *port,
|
||||||
|
ResetDB: *resetDb,
|
||||||
|
EnableRegistration: CheckRegistration(),
|
||||||
|
AllowedOrigins: allowedOrigins,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return &Config{
|
||||||
|
DBConnString: dbConnString,
|
||||||
|
ServerPort: *port,
|
||||||
|
ResetDB: *resetDb,
|
||||||
|
EnableRegistration: CheckRegistration(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,6 +143,26 @@ func UnpackDBConnString() (connInfo ConnectionInfo) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func unpackAllowedOrigin() []string {
|
||||||
|
allowedOriginsRaw := os.Getenv("ALLOWED_ORIGINS")
|
||||||
|
allowedOriginsSplit := strings.Split(allowedOriginsRaw, ",")
|
||||||
|
return allowedOriginsSplit
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckRegistration() bool {
|
||||||
|
if flagValue := os.Getenv("ENABLE_REGISTRATION"); flagValue != "" {
|
||||||
|
if val := strings.ToUpper(flagValue); val == "TRUE" {
|
||||||
|
return true
|
||||||
|
} else if val == "FALSE" {
|
||||||
|
return false
|
||||||
|
} else {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Config) GetDBConnString() string {
|
func (c *Config) GetDBConnString() string {
|
||||||
return c.DBConnString
|
return c.DBConnString
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-models/pkg/token"
|
"git.kundeng.us/phoenix/textsender-models/tx0/token"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/config"
|
"git.kundeng.us/phoenix/textsender-auth/internal/config"
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/model"
|
"git.kundeng.us/phoenix/textsender-auth/internal/store"
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/utility"
|
"git.kundeng.us/phoenix/textsender-auth/internal/utility"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -22,11 +22,12 @@ type LoginResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type LoginHandler struct {
|
type LoginHandler struct {
|
||||||
UserStore model.UserStore
|
Config *config.Config
|
||||||
|
UserStore store.UserStore
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLoginHandler(userStore model.UserStore) *LoginHandler {
|
func NewLoginHandler(cfg *config.Config, userStore store.UserStore) *LoginHandler {
|
||||||
return &LoginHandler{UserStore: userStore}
|
return &LoginHandler{Config: cfg, UserStore: userStore}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Login godoc
|
// Login godoc
|
||||||
|
|||||||
@@ -17,8 +17,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestLogin(t *testing.T) {
|
func TestLogin(t *testing.T) {
|
||||||
|
cfg := GetConfig()
|
||||||
mockstore := mock.NewMockUserStore()
|
mockstore := mock.NewMockUserStore()
|
||||||
handler := NewLoginHandler(mockstore)
|
handler := NewLoginHandler(cfg, mockstore)
|
||||||
|
|
||||||
testUser := GetTestUser()
|
testUser := GetTestUser()
|
||||||
unhashedPassword := testUser.Password
|
unhashedPassword := testUser.Password
|
||||||
|
|||||||
@@ -3,21 +3,21 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-models/pkg/token"
|
"git.kundeng.us/phoenix/textsender-models/tx0/token"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/config"
|
"git.kundeng.us/phoenix/textsender-auth/internal/config"
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/model"
|
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/store"
|
"git.kundeng.us/phoenix/textsender-auth/internal/store"
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/utility"
|
"git.kundeng.us/phoenix/textsender-auth/internal/utility"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RefreshHandler struct {
|
type RefreshHandler struct {
|
||||||
UserStore model.UserStore
|
Config *config.Config
|
||||||
|
UserStore store.UserStore
|
||||||
ServiceStore store.ServiceStore
|
ServiceStore store.ServiceStore
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRefreshHandler(userStore model.UserStore, serviceStore store.ServiceStore) *RefreshHandler {
|
func NewRefreshHandler(cfg *config.Config, userStore store.UserStore, serviceStore store.ServiceStore) *RefreshHandler {
|
||||||
return &RefreshHandler{UserStore: userStore, ServiceStore: serviceStore}
|
return &RefreshHandler{Config: cfg, UserStore: userStore, ServiceStore: serviceStore}
|
||||||
}
|
}
|
||||||
|
|
||||||
type RefreshRequest struct {
|
type RefreshRequest struct {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-models/pkg/user"
|
"git.kundeng.us/phoenix/textsender-models/tx0/user"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/handler/endpoint"
|
"git.kundeng.us/phoenix/textsender-auth/internal/handler/endpoint"
|
||||||
@@ -35,7 +35,8 @@ func TestRefreshTokenWithMock(t *testing.T) {
|
|||||||
assert.NoError(t, err, "Error creating service user: %v", err)
|
assert.NoError(t, err, "Error creating service user: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
handler := NewServiceHandler(mockStore)
|
cfg := GetConfig()
|
||||||
|
handler := NewServiceHandler(cfg, mockStore)
|
||||||
testService := ServiceLoginRequest{Username: serviceUser.Username, Passphrase: unhashed}
|
testService := ServiceLoginRequest{Username: serviceUser.Username, Passphrase: unhashed}
|
||||||
jsonValue, err := json.Marshal(testService)
|
jsonValue, err := json.Marshal(testService)
|
||||||
assert.NoError(t, err, "Error marshaling request")
|
assert.NoError(t, err, "Error marshaling request")
|
||||||
@@ -56,7 +57,7 @@ func TestRefreshTokenWithMock(t *testing.T) {
|
|||||||
jsonValue, err = json.Marshal(testReq)
|
jsonValue, err = json.Marshal(testReq)
|
||||||
assert.NoError(t, err, "Error marshaling request")
|
assert.NoError(t, err, "Error marshaling request")
|
||||||
|
|
||||||
newHandler := NewRefreshHandler(userStore, mockStore)
|
newHandler := NewRefreshHandler(cfg, userStore, mockStore)
|
||||||
req, _ = http.NewRequest("POST", endpoint.TokenRefresh, strings.NewReader(string(jsonValue)))
|
req, _ = http.NewRequest("POST", endpoint.TokenRefresh, strings.NewReader(string(jsonValue)))
|
||||||
rr = httptest.NewRecorder()
|
rr = httptest.NewRecorder()
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-models/pkg/user"
|
"git.kundeng.us/phoenix/textsender-models/tx0/user"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/model"
|
"git.kundeng.us/phoenix/textsender-auth/internal/config"
|
||||||
|
"git.kundeng.us/phoenix/textsender-auth/internal/store"
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/utility"
|
"git.kundeng.us/phoenix/textsender-auth/internal/utility"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,11 +30,12 @@ type RegisterResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UserHandler struct {
|
type UserHandler struct {
|
||||||
UserStore model.UserStore
|
Config *config.Config
|
||||||
|
UserStore store.UserStore
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserHandler(userStore model.UserStore) *UserHandler {
|
func NewUserHandler(cfg *config.Config, userStore store.UserStore) *UserHandler {
|
||||||
return &UserHandler{UserStore: userStore}
|
return &UserHandler{Config: cfg, UserStore: userStore}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register godoc
|
// Register godoc
|
||||||
@@ -46,6 +48,7 @@ func NewUserHandler(userStore model.UserStore) *UserHandler {
|
|||||||
// @Param request body RegisterUser true "Data to add user"
|
// @Param request body RegisterUser true "Data to add user"
|
||||||
// @Success 200 {object} RegisterResponse
|
// @Success 200 {object} RegisterResponse
|
||||||
// @Failure 400 {object} RegisterResponse
|
// @Failure 400 {object} RegisterResponse
|
||||||
|
// @Failure 403 {object} RegisterResponse
|
||||||
// @Failure 500 {object} RegisterResponse
|
// @Failure 500 {object} RegisterResponse
|
||||||
// @Router /register [post]
|
// @Router /register [post]
|
||||||
func (u *UserHandler) Register(w http.ResponseWriter, r *http.Request) {
|
func (u *UserHandler) Register(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -60,6 +63,12 @@ func (u *UserHandler) Register(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
var statusCode int
|
var statusCode int
|
||||||
var resp RegisterResponse
|
var resp RegisterResponse
|
||||||
|
if !u.Config.EnableRegistration {
|
||||||
|
statusCode = http.StatusForbidden
|
||||||
|
resp.Message = "Registration disabled"
|
||||||
|
RespondWithJson(w, statusCode, &resp)
|
||||||
|
return
|
||||||
|
}
|
||||||
user := user.User{Username: req.Username, Password: req.Password, PhoneNumber: req.PhoneNumber}
|
user := user.User{Username: req.Username, Password: req.Password, PhoneNumber: req.PhoneNumber}
|
||||||
|
|
||||||
fmt.Println("Username:", user.Username)
|
fmt.Println("Username:", user.Username)
|
||||||
|
|||||||
@@ -14,8 +14,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestCreateUserWithMock(t *testing.T) {
|
func TestCreateUserWithMock(t *testing.T) {
|
||||||
|
cfg := GetConfig()
|
||||||
mockstore := mock.NewMockUserStore()
|
mockstore := mock.NewMockUserStore()
|
||||||
handler := NewUserHandler(mockstore)
|
handler := NewUserHandler(cfg, mockstore)
|
||||||
|
|
||||||
testUser := GetTestUser()
|
testUser := GetTestUser()
|
||||||
jsonValue, _ := json.Marshal(testUser)
|
jsonValue, _ := json.Marshal(testUser)
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-models/pkg/token"
|
"git.kundeng.us/phoenix/textsender-models/tx0/token"
|
||||||
"git.kundeng.us/phoenix/textsender-models/pkg/user"
|
"git.kundeng.us/phoenix/textsender-models/tx0/user"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/config"
|
"git.kundeng.us/phoenix/textsender-auth/internal/config"
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/store"
|
"git.kundeng.us/phoenix/textsender-auth/internal/store"
|
||||||
@@ -12,11 +12,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ServiceHandler struct {
|
type ServiceHandler struct {
|
||||||
|
Config *config.Config
|
||||||
ServiceStore store.ServiceStore
|
ServiceStore store.ServiceStore
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServiceHandler(serviceStore store.ServiceStore) *ServiceHandler {
|
func NewServiceHandler(cfg *config.Config, serviceStore store.ServiceStore) *ServiceHandler {
|
||||||
return &ServiceHandler{ServiceStore: serviceStore}
|
return &ServiceHandler{Config: cfg, ServiceStore: serviceStore}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServiceCreationRequest struct {
|
type ServiceCreationRequest struct {
|
||||||
@@ -39,6 +40,7 @@ type ServiceCreationResponse struct {
|
|||||||
// @Param request body ServiceCreationRequest true "Data to add user"
|
// @Param request body ServiceCreationRequest true "Data to add user"
|
||||||
// @Success 200 {object} ServiceCreationResponse
|
// @Success 200 {object} ServiceCreationResponse
|
||||||
// @Failure 400 {object} ServiceCreationResponse
|
// @Failure 400 {object} ServiceCreationResponse
|
||||||
|
// @Failure 403 {object} ServiceCreationResponse
|
||||||
// @Failure 500 {object} ServiceCreationResponse
|
// @Failure 500 {object} ServiceCreationResponse
|
||||||
// @Router /service/register [post]
|
// @Router /service/register [post]
|
||||||
func (s *ServiceHandler) Register(w http.ResponseWriter, r *http.Request) {
|
func (s *ServiceHandler) Register(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -52,6 +54,12 @@ func (s *ServiceHandler) Register(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
var statusCode int
|
var statusCode int
|
||||||
var resp ServiceCreationResponse
|
var resp ServiceCreationResponse
|
||||||
|
if !s.Config.EnableRegistration {
|
||||||
|
statusCode = http.StatusForbidden
|
||||||
|
resp.Message = "Registration disabled"
|
||||||
|
RespondWithJson(w, statusCode, &resp)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
if exists, err := s.ServiceStore.CheckWithUsername(ctx, req.Username); err != nil {
|
if exists, err := s.ServiceStore.CheckWithUsername(ctx, req.Username); err != nil {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-models/pkg/user"
|
"git.kundeng.us/phoenix/textsender-models/tx0/user"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/handler/endpoint"
|
"git.kundeng.us/phoenix/textsender-auth/internal/handler/endpoint"
|
||||||
@@ -16,8 +16,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestCreateServiceUserWithMock(t *testing.T) {
|
func TestCreateServiceUserWithMock(t *testing.T) {
|
||||||
|
cfg := GetConfig()
|
||||||
mockStore := mock.NewMockServiceUserStore()
|
mockStore := mock.NewMockServiceUserStore()
|
||||||
handler := NewServiceHandler(mockStore)
|
handler := NewServiceHandler(cfg, mockStore)
|
||||||
|
|
||||||
testService := ServiceCreationRequest{Username: "swoon", Passphrase: "ewrewr329n12y3x2!2"}
|
testService := ServiceCreationRequest{Username: "swoon", Passphrase: "ewrewr329n12y3x2!2"}
|
||||||
jsonValue, err := json.Marshal(testService)
|
jsonValue, err := json.Marshal(testService)
|
||||||
@@ -53,7 +54,8 @@ func TestLoginServiceUserWithMock(t *testing.T) {
|
|||||||
assert.NoError(t, err, "Error creating service user: %v", err)
|
assert.NoError(t, err, "Error creating service user: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
handler := NewServiceHandler(mockStore)
|
cfg := GetConfig()
|
||||||
|
handler := NewServiceHandler(cfg, mockStore)
|
||||||
testService := ServiceLoginRequest{Username: serviceUser.Username, Passphrase: unhashed}
|
testService := ServiceLoginRequest{Username: serviceUser.Username, Passphrase: unhashed}
|
||||||
jsonValue, err := json.Marshal(testService)
|
jsonValue, err := json.Marshal(testService)
|
||||||
assert.NoError(t, err, "Error marshaling request")
|
assert.NoError(t, err, "Error marshaling request")
|
||||||
|
|||||||
@@ -1,9 +1,40 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.kundeng.us/phoenix/textsender-models/pkg/user"
|
"log"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
|
||||||
|
"git.kundeng.us/phoenix/textsender-models/tx0/user"
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
|
||||||
|
"git.kundeng.us/phoenix/textsender-auth/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetTestUser() user.User {
|
func GetTestUser() user.User {
|
||||||
return user.User{Username: "ghost", PhoneNumber: "+1234567890", Password: "dfgdffddfd"}
|
return user.User{Username: "ghost", PhoneNumber: "+1234567890", Password: "dfgdffddfd"}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetConfig() *config.Config {
|
||||||
|
err := godotenv.Load()
|
||||||
|
if err != nil {
|
||||||
|
cwd, _ := os.Getwd()
|
||||||
|
envPath := path.Join(cwd, "../..", ".env")
|
||||||
|
if err = godotenv.Load(envPath); err != nil {
|
||||||
|
prevPath := path.Join(envPath, "../..", ".env")
|
||||||
|
if err = godotenv.Load(prevPath); err != nil {
|
||||||
|
log.Fatal("Error loading .env file")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unpackedConnString := config.UnpackDBConnString()
|
||||||
|
dbConnString := unpackedConnString.Parse()
|
||||||
|
|
||||||
|
return &config.Config{
|
||||||
|
DBConnString: dbConnString,
|
||||||
|
ServerPort: config.Port,
|
||||||
|
ResetDB: false,
|
||||||
|
EnableRegistration: config.CheckRegistration(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-models/pkg/user"
|
"git.kundeng.us/phoenix/textsender-models/tx0/user"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-models/pkg/user"
|
"git.kundeng.us/phoenix/textsender-models/tx0/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockUserStore struct {
|
type MockUserStore struct {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-models/pkg/user"
|
"git.kundeng.us/phoenix/textsender-models/tx0/user"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package model
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -8,10 +8,9 @@ import (
|
|||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-models/pkg/user"
|
"git.kundeng.us/phoenix/textsender-models/tx0/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type UserStore interface {
|
type UserStore interface {
|
||||||
CreateUser(ctx context.Context, user *user.User) error
|
CreateUser(ctx context.Context, user *user.User) error
|
||||||
GetUserByID(ctx context.Context, id uuid.UUID) (*user.User, error)
|
GetUserByID(ctx context.Context, id uuid.UUID) (*user.User, error)
|
||||||
@@ -4,19 +4,19 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"git.kundeng.us/phoenix/textsender-models/tx0/token"
|
||||||
|
"git.kundeng.us/phoenix/textsender-models/tx0/user"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-auth/internal/config"
|
"git.kundeng.us/phoenix/textsender-auth/internal/config"
|
||||||
"git.kundeng.us/phoenix/textsender-models/pkg/token"
|
|
||||||
"git.kundeng.us/phoenix/textsender-models/pkg/user"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const ROLE_TYPE = "regular"
|
const ROLE_TYPE = "regular"
|
||||||
const TOKEN_TYPE = "Bearer"
|
const TOKEN_TYPE = "Bearer"
|
||||||
|
|
||||||
type TokenGenerator struct {
|
type TokenGenerator struct {
|
||||||
SecretKey []byte
|
SecretKey []byte
|
||||||
hourOffset time.Duration
|
hourOffset time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ func (t *TokenGenerator) GenerateToken(usr any) (*token.Login, error) {
|
|||||||
if tokenString, err := myToken.SignedString(t.SecretKey); err != nil {
|
if tokenString, err := myToken.SignedString(t.SecretKey); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
return &token.Login{AccessToken: tokenString, TokenType: TOKEN_TYPE, ExpiresIn: expirationTime.Unix()}, nil
|
return &token.Login{UserId: claims.UserId, AccessToken: tokenString, TokenType: TOKEN_TYPE, ExpiresIn: expirationTime.Unix()}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -97,11 +97,10 @@ func (t *TokenGenerator) ExtractIdFromToken(accessToken string) (uuid.UUID, erro
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (t *TokenGenerator) parseTokenWithClaims(accessToken string, claims *token.Claims) (*jwt.Token, error) {
|
func (t *TokenGenerator) parseTokenWithClaims(accessToken string, claims *token.Claims) (*jwt.Token, error) {
|
||||||
tken, err := jwt.ParseWithClaims(accessToken, claims, func(tken *jwt.Token) (any, error) {
|
tken, err := jwt.ParseWithClaims(accessToken, claims, func(tken *jwt.Token) (any, error) {
|
||||||
if _, ok := tken.Method.(*jwt.SigningMethodHMAC); !ok {
|
if _, ok := tken.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||||
return nil, fmt.Errorf("unexpected signing method: %v", tken.Header["alg"])
|
return nil, fmt.Errorf("unexpected signing method: %v", tken.Header["alg"])
|
||||||
}
|
}
|
||||||
return t.SecretKey, nil
|
return t.SecretKey, nil
|
||||||
}, jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}))
|
}, jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}))
|
||||||
|
|||||||
Reference in New Issue
Block a user