next_changes (#32)

Reviewed-on: phoenix/textsender-auth#32
This commit is contained in:
phoenix
2025-12-22 22:42:45 +00:00
8 changed files with 46 additions and 13 deletions
+1
View File
@@ -6,3 +6,4 @@ DB_HOST=auth_db
DB_PORT=5432
DB_SSLMODE=disable
ENABLE_REGISTRATION=true
ALLOWED_ORIGINS="http://textsender.com"
+1
View File
@@ -6,3 +6,4 @@ DB_HOST=localhost
DB_PORT=5432
DB_SSLMODE=disable
ENABLE_REGISTRATION=true
ALLOWED_ORIGINS="http://textsender.com"
+1
View File
@@ -107,6 +107,7 @@ jobs:
echo "DB_PORT=$DB_PORT" >> .env
echo "DB_SSLMODE=$DB_SSLMODE" >> .env
echo "ENABLE_REGISTRATION=true" >> .env
echo "ALLOWED_ORIGINS=http://localhost:9080" >> .env
echo "Initializing config"
mkdir -p ~/.ssh
+10
View File
@@ -12,6 +12,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/swaggo/http-swagger/v2"
_ "git.kundeng.us/phoenix/textsender-auth/docs"
@@ -82,6 +83,15 @@ func main() {
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.Recoverer)
router.Use(middleware.Timeout(60 * time.Second))
+2 -1
View File
@@ -3,8 +3,9 @@ module git.kundeng.us/phoenix/textsender-auth
go 1.25.4
require (
git.kundeng.us/phoenix/textsender-models v0.0.10
git.kundeng.us/phoenix/textsender-models v0.0.12
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/google/uuid v1.6.0
github.com/gorilla/mux v1.8.1
+4 -2
View File
@@ -1,5 +1,5 @@
git.kundeng.us/phoenix/textsender-models v0.0.10 h1:0AMe/zm4Xte2BcuqqhRP7+42m+4To+rUnHkR2a9eXlI=
git.kundeng.us/phoenix/textsender-models v0.0.10/go.mod h1:9iPDQJg1Tc6WMNoW5+f8YKmnosMwlWHJ++hmxNLDEe0=
git.kundeng.us/phoenix/textsender-models v0.0.12 h1:ps9H3FS5LyCwQhAiIvg4vYyfLZZ64dex1y9ytb0o9C4=
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/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
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/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/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.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
+23 -5
View File
@@ -19,6 +19,7 @@ type Config struct {
ServerPort string
ResetDB bool
EnableRegistration bool
AllowedOrigins []string
}
type ConnectionInfo struct {
@@ -67,12 +68,23 @@ func Load() *Config {
unpackedConnString := UnpackDBConnString()
dbConnString := unpackedConnString.Parse()
allowedOrigins := unpackAllowedOrigin()
return &Config{
DBConnString: dbConnString,
ServerPort: *port,
ResetDB: *resetDb,
EnableRegistration: CheckRegistration(),
if len(allowedOrigins) > 0 {
return &Config{
DBConnString: dbConnString,
ServerPort: *port,
ResetDB: *resetDb,
EnableRegistration: CheckRegistration(),
AllowedOrigins: allowedOrigins,
}
} else {
return &Config{
DBConnString: dbConnString,
ServerPort: *port,
ResetDB: *resetDb,
EnableRegistration: CheckRegistration(),
}
}
}
@@ -131,6 +143,12 @@ func UnpackDBConnString() (connInfo ConnectionInfo) {
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" {
+4 -5
View File
@@ -6,8 +6,8 @@ import (
"git.kundeng.us/phoenix/textsender-models/tx0/token"
"git.kundeng.us/phoenix/textsender-models/tx0/user"
"github.com/google/uuid"
"github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
"git.kundeng.us/phoenix/textsender-auth/internal/config"
)
@@ -16,7 +16,7 @@ const ROLE_TYPE = "regular"
const TOKEN_TYPE = "Bearer"
type TokenGenerator struct {
SecretKey []byte
SecretKey []byte
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 {
return nil, err
} 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) {
tken, err := jwt.ParseWithClaims(accessToken, claims, func(tken *jwt.Token) (any, error) {
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
}, jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}))