mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
make JWT secret and expiry time configurable
This commit is contained in:
parent
e383df74ab
commit
c404c7af5a
4 changed files with 95 additions and 71 deletions
|
@ -59,6 +59,8 @@ func InitConfig() error {
|
||||||
s3Region = flag.String("s3-region", "default", "S3 Region for file uploads")
|
s3Region = flag.String("s3-region", "default", "S3 Region for file uploads")
|
||||||
s3NoSSL = flag.Bool("s3-nossl", false, "Use encrypted connections to the S3 API")
|
s3NoSSL = flag.Bool("s3-nossl", false, "Use encrypted connections to the S3 API")
|
||||||
s3PathStyle = flag.Bool("s3-pathstyle", false, "Use path-style S3 API")
|
s3PathStyle = flag.Bool("s3-pathstyle", false, "Use path-style S3 API")
|
||||||
|
jwtSecret = flag.String("jwt-secret", "This should NOT be here!!@33$8&", "The JSON Web Token secret")
|
||||||
|
jwtExpiresAfter = flag.String("jwt-expires-after", "1w", "The time after which the JSON Web Token expires")
|
||||||
)
|
)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
@ -81,6 +83,8 @@ func InitConfig() error {
|
||||||
"s3.bucket": *s3Bucket,
|
"s3.bucket": *s3Bucket,
|
||||||
"s3.endpoint": *s3Endpoint,
|
"s3.endpoint": *s3Endpoint,
|
||||||
"s3.region": *s3Region,
|
"s3.region": *s3Region,
|
||||||
|
"jwt.secret": *jwtSecret,
|
||||||
|
"jwt.expires-after": *jwtExpiresAfter,
|
||||||
}
|
}
|
||||||
|
|
||||||
if *s3NoSSL == true {
|
if *s3NoSSL == true {
|
||||||
|
@ -116,6 +120,8 @@ func InitConfig() error {
|
||||||
"S3_REGION": "s3.region",
|
"S3_REGION": "s3.region",
|
||||||
"S3_NOSSL": "s3.nossl",
|
"S3_NOSSL": "s3.nossl",
|
||||||
"S3_PATHSTYLE": "s3.pathstyle",
|
"S3_PATHSTYLE": "s3.pathstyle",
|
||||||
|
"JWT_SECRET": "jwt.secret",
|
||||||
|
"JWT_EXPIRES_AFTER": "jwt.expires-after",
|
||||||
}
|
}
|
||||||
|
|
||||||
defaults := config.NewStatic(static)
|
defaults := config.NewStatic(static)
|
||||||
|
|
|
@ -22,11 +22,13 @@
|
||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration"
|
||||||
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
|
||||||
"github.com/dgrijalva/jwt-go"
|
"github.com/dgrijalva/jwt-go"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type tokenClaims struct {
|
type tokenClaims struct {
|
||||||
|
@ -86,12 +88,30 @@ func authenticate(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expiresStr, err := configuration.GolbalConfig.String("jwt.expires-after")
|
||||||
|
if err != nil {
|
||||||
|
helper.UnauthorizedError(c, "Backend configuration error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
expiresDuration, err := time.ParseDuration(expiresStr)
|
||||||
|
if err != nil {
|
||||||
|
helper.UnauthorizedError(c, "Backend configuration error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
secret, err := configuration.GolbalConfig.String("jwt.secret")
|
||||||
|
if err != nil {
|
||||||
|
helper.UnauthorizedError(c, "Backend configuration error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// create authentication token
|
// create authentication token
|
||||||
claims := tokenClaims{
|
claims := tokenClaims{
|
||||||
user.ID,
|
user.ID,
|
||||||
user.Role,
|
user.Role,
|
||||||
jwt.StandardClaims{
|
jwt.StandardClaims{
|
||||||
ExpiresAt: time.Now().Add(weekHours).Unix(),
|
ExpiresAt: time.Now().Add(expiresDuration).Unix(),
|
||||||
IssuedAt: time.Now().Unix(),
|
IssuedAt: time.Now().Unix(),
|
||||||
Issuer: "http://web.villas.fein-aachen.org/",
|
Issuer: "http://web.villas.fein-aachen.org/",
|
||||||
},
|
},
|
||||||
|
@ -99,7 +119,7 @@ func authenticate(c *gin.Context) {
|
||||||
|
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
|
|
||||||
tokenString, err := token.SignedString([]byte(jwtSigningSecret))
|
tokenString, err := token.SignedString([]byte(secret))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.InternalServerError(c, err.Error())
|
helper.InternalServerError(c, err.Error())
|
||||||
return
|
return
|
||||||
|
|
|
@ -23,20 +23,16 @@ package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: the signing secret must be environmental variable
|
|
||||||
const jwtSigningSecret = "This should NOT be here!!@33$8&"
|
|
||||||
const weekHours = time.Hour * 24 * 7
|
|
||||||
|
|
||||||
func RegisterUserEndpoints(r *gin.RouterGroup) {
|
func RegisterUserEndpoints(r *gin.RouterGroup) {
|
||||||
r.POST("", addUser)
|
r.POST("", addUser)
|
||||||
r.PUT("/:userID", updateUser)
|
r.PUT("/:userID", updateUser)
|
||||||
|
|
|
@ -24,8 +24,10 @@ package user
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration"
|
||||||
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
|
||||||
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
|
||||||
|
|
||||||
"github.com/dgrijalva/jwt-go"
|
"github.com/dgrijalva/jwt-go"
|
||||||
"github.com/dgrijalva/jwt-go/request"
|
"github.com/dgrijalva/jwt-go/request"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -66,8 +68,8 @@ func Authentication(unauthorized bool) gin.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
// return secret in byte format
|
// return secret in byte format
|
||||||
secret := ([]byte(jwtSigningSecret))
|
secret, _ := configuration.GolbalConfig.String("jwt.secret")
|
||||||
return secret, nil
|
return []byte(secret), nil
|
||||||
})
|
})
|
||||||
|
|
||||||
// If the authentication extraction fails return HTTP CODE 401
|
// If the authentication extraction fails return HTTP CODE 401
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue