mirror of
https://github.com/KaySar12/NextZen-UserService.git
synced 2025-10-06 19:59:42 +07:00
feature:Save OIDC setting in DB (TODO: support multiple profile)
This commit is contained in:
@@ -41,6 +41,8 @@ func InitRouter() *gin.Engine {
|
||||
r.POST("/v1/users/oidc/validateToken", v1.OIDCValidateToken)
|
||||
r.POST("/v1/users/oidc/logout", v1.OIDCLogout)
|
||||
r.GET("/v1/users/oidc/health", v1.OIDCHealthCheck)
|
||||
r.GET("/v1/users/oidc/settings", v1.GetOIDCSettings)
|
||||
r.POST("/v1/users/oidc/saveSettings", v1.SaveOIDCSettings)
|
||||
v1Group := r.Group("/v1")
|
||||
|
||||
v1Group.Use(jwt.JWT(
|
||||
|
||||
+63
-3
@@ -51,10 +51,20 @@ var (
|
||||
clientSecret = "PE05fcDP4qESUmyZ1TNYpZNBxRPq70VpFI81vehsoJ6WhGz5yPXMljrFrOdMRdRhrYmF03fHWTZHgO9ZdNENrLN13BzL8CAgtEkTsyjXfgx9GvISheIjYfpSfvo219fL"
|
||||
authURL = "http://accessmanager.local/application/o/nextzenos-oidc/"
|
||||
//authURL = "http://10.0.0.26:9000/application/o/nextzenos-oidc/"
|
||||
//callbackURL = "http://nextzenos.local/v1/users/oidc/callback"
|
||||
callbackURL = "http://172.20.60.244:8080/v1/users/oidc/callback"
|
||||
callbackURL = "http://nextzenos.local/v1/users/oidc/callback"
|
||||
//callbackURL = "http://172.20.60.244:8080/v1/users/oidc/callback"
|
||||
)
|
||||
|
||||
type OIDCSetting struct {
|
||||
Settings struct {
|
||||
ClientID string `json:"clientId"`
|
||||
ClientSecret string `json:"clientSecret"`
|
||||
Issuer string `json:"issuer"`
|
||||
AuthURL string `json:"authUrl"`
|
||||
CallbackURL string `json:"callbackUrl"`
|
||||
} `json:"settings"`
|
||||
}
|
||||
|
||||
// @Summary register user
|
||||
// @Router /user/register/ [post]
|
||||
func PostUserRegister(c *gin.Context) {
|
||||
@@ -246,7 +256,7 @@ func CheckOIDCInit() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if !oidcInit {
|
||||
log.Println("Provider is Offline")
|
||||
c.JSON(http.StatusServiceUnavailable, model.Result{Success: http.StatusProxyAuthRequired, Message: "Authentik Server is Offline"})
|
||||
c.JSON(http.StatusServiceUnavailable, model.Result{Success: common_err.OIDC_OFFLINE, Message: "Authentik Server is Offline"})
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
@@ -255,6 +265,14 @@ func CheckOIDCInit() gin.HandlerFunc {
|
||||
|
||||
// Use an init function to initialize the oauth2Config variable.
|
||||
func OIDC() error {
|
||||
authentik, err := service.MyService.Authentik().GetSettings()
|
||||
if (authentik != model2.AuthentikCredentialsDBModel{} && err == nil) {
|
||||
clientID = authentik.ClientID
|
||||
clientSecret = authentik.ClientSecret
|
||||
authServer = authentik.Issuer
|
||||
authURL = authentik.AuthUrl
|
||||
callbackURL = authentik.CallbackUrl
|
||||
}
|
||||
ctx := context.Background()
|
||||
provider, err := oidc.NewProvider(ctx, authURL)
|
||||
if err != nil {
|
||||
@@ -270,6 +288,24 @@ func OIDC() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func GetOIDCSettings(c *gin.Context) {
|
||||
authentik, err := service.MyService.Authentik().GetSettings()
|
||||
if err != nil {
|
||||
c.JSON(common_err.SERVICE_ERROR,
|
||||
model.Result{
|
||||
Success: common_err.SERVICE_ERROR,
|
||||
Message: common_err.GetMsg(common_err.SERVICE_ERROR),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(common_err.SUCCESS,
|
||||
model.Result{
|
||||
Success: common_err.SUCCESS,
|
||||
Message: common_err.GetMsg(common_err.SUCCESS),
|
||||
Data: authentik,
|
||||
})
|
||||
return
|
||||
}
|
||||
func OIDCLogin(c *gin.Context) {
|
||||
json := make(map[string]string)
|
||||
c.ShouldBind(&json)
|
||||
@@ -285,6 +321,30 @@ func OIDCLogin(c *gin.Context) {
|
||||
Data: oauth2Config.AuthCodeURL(state),
|
||||
})
|
||||
}
|
||||
func SaveOIDCSettings(c *gin.Context) {
|
||||
var oidcSetting OIDCSetting
|
||||
var authentik model2.AuthentikCredentialsDBModel
|
||||
c.ShouldBind(&oidcSetting)
|
||||
authentik.ClientID = oidcSetting.Settings.ClientID
|
||||
authentik.ClientSecret = oidcSetting.Settings.ClientSecret
|
||||
authentik.Issuer = oidcSetting.Settings.Issuer
|
||||
authentik.AuthUrl = oidcSetting.Settings.AuthURL
|
||||
authentik.CallbackUrl = oidcSetting.Settings.CallbackURL
|
||||
var result, err = service.MyService.Authentik().UpdateSettings(authentik)
|
||||
if err != nil {
|
||||
c.JSON(common_err.SERVICE_ERROR,
|
||||
model.Result{
|
||||
Success: common_err.SERVICE_ERROR,
|
||||
Message: common_err.GetMsg(common_err.SERVICE_ERROR),
|
||||
})
|
||||
}
|
||||
c.JSON(common_err.SUCCESS,
|
||||
model.Result{
|
||||
Success: common_err.SUCCESS,
|
||||
Message: common_err.GetMsg(common_err.SUCCESS),
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
func OIDCCallback(c *gin.Context) {
|
||||
w := c.Writer
|
||||
r := c.Request
|
||||
|
||||
Reference in New Issue
Block a user