adjust response keycloak validation
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"antrian-operasi/internal/shared"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -36,9 +37,15 @@ func AuthKeycloak() (gin.HandlerFunc, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
|
errorResponse := shared.BaseErrorResponse{
|
||||||
|
Success: false,
|
||||||
|
Code: 401,
|
||||||
|
}
|
||||||
|
|
||||||
auth := c.GetHeader("Authorization")
|
auth := c.GetHeader("Authorization")
|
||||||
if auth == "" {
|
if auth == "" {
|
||||||
c.AbortWithStatusJSON(401, gin.H{"message": "missing token"})
|
errorResponse.Message = "missing token"
|
||||||
|
c.AbortWithStatusJSON(http.StatusUnauthorized, errorResponse)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +53,8 @@ func AuthKeycloak() (gin.HandlerFunc, error) {
|
|||||||
|
|
||||||
token, err := jwt.Parse(tokenStr, jwks.Keyfunc)
|
token, err := jwt.Parse(tokenStr, jwks.Keyfunc)
|
||||||
if err != nil || !token.Valid {
|
if err != nil || !token.Valid {
|
||||||
c.AbortWithStatusJSON(401, gin.H{"message": err.Error()})
|
errorResponse.Message = err.Error()
|
||||||
|
c.AbortWithStatusJSON(http.StatusUnauthorized, errorResponse)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,14 +62,17 @@ func AuthKeycloak() (gin.HandlerFunc, error) {
|
|||||||
log.Println(claims)
|
log.Println(claims)
|
||||||
|
|
||||||
// validate issuer
|
// validate issuer
|
||||||
|
errorResponse.Message = "invalid keycloak configuration"
|
||||||
if claims["iss"] != issuer {
|
if claims["iss"] != issuer {
|
||||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid issuer"})
|
errorResponse.Errors = []string{"invalid issuer"}
|
||||||
|
c.AbortWithStatusJSON(http.StatusUnauthorized, errorResponse)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate audience
|
// validate audience
|
||||||
if !claims.VerifyAudience(audience, true) {
|
if !claims.VerifyAudience(audience, true) {
|
||||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid audience"})
|
errorResponse.Errors = []string{"invalid audience"}
|
||||||
|
c.AbortWithStatusJSON(http.StatusUnauthorized, errorResponse)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user