Skip to content

Commit

Permalink
refactor: casdoor settings
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Dec 3, 2023
1 parent ed72b67 commit 37fa8b8
Show file tree
Hide file tree
Showing 12 changed files with 1,827 additions and 544 deletions.
208 changes: 103 additions & 105 deletions api/user/auth.go
Original file line number Diff line number Diff line change
@@ -1,131 +1,129 @@
package user

import (
"fmt"
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/settings"
"net/http"
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/settings"
"net/http"

"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)

type LoginUser struct {
Name string `json:"name" binding:"required,max=255"`
Password string `json:"password" binding:"required,max=255"`
Name string `json:"name" binding:"required,max=255"`
Password string `json:"password" binding:"required,max=255"`
}

func Login(c *gin.Context) {
var user LoginUser
ok := api.BindAndValid(c, &user)
if !ok {
return
}
var user LoginUser
ok := api.BindAndValid(c, &user)
if !ok {
return
}

u, _ := model.GetUser(user.Name)
u, _ := model.GetUser(user.Name)

if err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(user.Password)); err != nil {
c.JSON(http.StatusForbidden, gin.H{
"message": "The username or password is incorrect",
})
return
}
if err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(user.Password)); err != nil {
c.JSON(http.StatusForbidden, gin.H{
"message": "The username or password is incorrect",
})
return
}

token, err := model.GenerateJWT(u.Name)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}
token, err := model.GenerateJWT(u.Name)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}

c.JSON(http.StatusOK, gin.H{
"message": "ok",
"token": token,
})
c.JSON(http.StatusOK, gin.H{
"message": "ok",
"token": token,
})
}

func Logout(c *gin.Context) {
token := c.GetHeader("Authorization")
if token != "" {
err := model.DeleteToken(token)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}
}
c.JSON(http.StatusNoContent, nil)
token := c.GetHeader("Authorization")
if token != "" {
err := model.DeleteToken(token)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}
}
c.JSON(http.StatusNoContent, nil)
}

type CasdoorLoginUser struct {
Code string `json:"code" binding:"required,max=255"`
State string `json:"state" binding:"required,max=255"`
Code string `json:"code" binding:"required,max=255"`
State string `json:"state" binding:"required,max=255"`
}

func CasdoorCallback(c *gin.Context) {
var loginUser CasdoorLoginUser
fmt.Println("CasdoorCallback called")
ok := api.BindAndValid(c, &loginUser)
if !ok {
return
}
endpoint := settings.ServerSettings.CasdoorEndpoint
clientId := settings.ServerSettings.CasdoorClientId
clientSecret := settings.ServerSettings.CasdoorClientSecret
certificate := settings.ServerSettings.CasdoorCertificate
organization := settings.ServerSettings.CasdoorOrganization
application := settings.ServerSettings.CasdoorApplication
if endpoint == "" || clientId == "" || clientSecret == "" || certificate == "" || organization == "" || application == "" {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Casdoor is not configured",
})
return
}
casdoorsdk.InitConfig(endpoint, clientId, clientSecret, certificate, organization, application)
token, err := casdoorsdk.GetOAuthToken(loginUser.Code, loginUser.State)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}
claims, err := casdoorsdk.ParseJwtToken(token.AccessToken)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}
u, err := model.GetUser(claims.Name)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.JSON(http.StatusForbidden, gin.H{
"message": "User not exist",
})
} else {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
}
return
}
var loginUser CasdoorLoginUser
ok := api.BindAndValid(c, &loginUser)
if !ok {
return
}
endpoint := settings.CasdoorSettings.Endpoint
clientId := settings.CasdoorSettings.ClientId
clientSecret := settings.CasdoorSettings.ClientSecret
certificate := settings.CasdoorSettings.Certificate
organization := settings.CasdoorSettings.Organization
application := settings.CasdoorSettings.Application
if endpoint == "" || clientId == "" || clientSecret == "" || certificate == "" || organization == "" || application == "" {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Casdoor is not configured",
})
return
}
casdoorsdk.InitConfig(endpoint, clientId, clientSecret, certificate, organization, application)
token, err := casdoorsdk.GetOAuthToken(loginUser.Code, loginUser.State)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}
claims, err := casdoorsdk.ParseJwtToken(token.AccessToken)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}
u, err := model.GetUser(claims.Name)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.JSON(http.StatusForbidden, gin.H{
"message": "User not exist",
})
} else {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
}
return
}

userToken, err := model.GenerateJWT(u.Name)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}
userToken, err := model.GenerateJWT(u.Name)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}

c.JSON(http.StatusOK, gin.H{
"message": "ok",
"token": userToken,
})
c.JSON(http.StatusOK, gin.H{
"message": "ok",
"token": userToken,
})
}
19 changes: 9 additions & 10 deletions api/user/casdoor.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package user

import (
"fmt"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/gin-gonic/gin"
"net/http"
"net/url"
"fmt"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/gin-gonic/gin"
"net/http"
"net/url"
)

func GetCasdoorUri(c *gin.Context) {
endpoint := settings.ServerSettings.CasdoorEndpoint
clientId := settings.ServerSettings.CasdoorClientId
redirectUri := settings.ServerSettings.CasdoorRedirectUri
state := settings.ServerSettings.CasdoorApplication
fmt.Println(redirectUri)
endpoint := settings.CasdoorSettings.Endpoint
clientId := settings.CasdoorSettings.ClientId
redirectUri := settings.CasdoorSettings.RedirectUri
state := settings.CasdoorSettings.Application
if endpoint == "" || clientId == "" || redirectUri == "" || state == "" {
c.JSON(http.StatusOK, gin.H{
"uri": "",
Expand Down
21 changes: 8 additions & 13 deletions app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ StartCmd = login
Database = database
CADir =
Demo =
CasdoorEndpoint =
CasdoorClientId =
CasdoorClientSecret =
CasdoorCertificate =
CasdoorOrganization =
CasdoorApplication =
CasdoorRedirectUri =

[nginx]
AccessLogPath = /var/log/nginx/access.log
Expand All @@ -31,9 +24,11 @@ BaseUrl =
Proxy =
Token =

[git]
Url =
AuthMethod =
Username =
Password =
PrivateKeyFile =
[casdoor]
Endpoint =
ClientId =
ClientSecret =
Certificate =
Organization =
Application =
RedirectUri =
1 change: 0 additions & 1 deletion app/.idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/.idea/jsLinters/eslint.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 37fa8b8

Please sign in to comment.