chore: add type define for casdoor module

This commit is contained in:
0xJacky 2023-12-03 11:34:25 +08:00
parent 37fa8b899f
commit 6dd0ec46a3
No known key found for this signature in database
GPG key ID: B6E4A6E4A561BAF0
7 changed files with 568 additions and 605 deletions

View file

@ -1,129 +1,64 @@
package user package user
import ( import (
"github.com/0xJacky/Nginx-UI/api" "github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/model" "github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/settings" "net/http"
"net/http"
"github.com/casdoor/casdoor-go-sdk/casdoorsdk" "github.com/gin-gonic/gin"
"github.com/gin-gonic/gin" "golang.org/x/crypto/bcrypt"
"github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
) )
type LoginUser struct { type LoginUser struct {
Name string `json:"name" binding:"required,max=255"` Name string `json:"name" binding:"required,max=255"`
Password string `json:"password" binding:"required,max=255"` Password string `json:"password" binding:"required,max=255"`
}
type LoginResponse struct {
Message string `json:"message"`
Token string `json:"token"`
} }
func Login(c *gin.Context) { func Login(c *gin.Context) {
var user LoginUser var user LoginUser
ok := api.BindAndValid(c, &user) ok := api.BindAndValid(c, &user)
if !ok { if !ok {
return return
} }
u, _ := model.GetUser(user.Name) u, _ := model.GetUser(user.Name)
if err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(user.Password)); err != nil { if err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(user.Password)); err != nil {
c.JSON(http.StatusForbidden, gin.H{ c.JSON(http.StatusForbidden, gin.H{
"message": "The username or password is incorrect", "message": "The username or password is incorrect",
}) })
return return
} }
token, err := model.GenerateJWT(u.Name) token, err := model.GenerateJWT(u.Name)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{ c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(), "message": err.Error(),
}) })
return return
} }
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, LoginResponse{
"message": "ok", Message: "ok",
"token": token, Token: token,
}) })
} }
func Logout(c *gin.Context) { func Logout(c *gin.Context) {
token := c.GetHeader("Authorization") token := c.GetHeader("Authorization")
if token != "" { if token != "" {
err := model.DeleteToken(token) err := model.DeleteToken(token)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{ c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(), "message": err.Error(),
}) })
return return
} }
} }
c.JSON(http.StatusNoContent, nil) c.JSON(http.StatusNoContent, nil)
}
type CasdoorLoginUser struct {
Code string `json:"code" binding:"required,max=255"`
State string `json:"state" binding:"required,max=255"`
}
func CasdoorCallback(c *gin.Context) {
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
}
c.JSON(http.StatusOK, gin.H{
"message": "ok",
"token": userToken,
})
} }

View file

@ -2,24 +2,96 @@ package user
import ( import (
"fmt" "fmt"
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/settings" "github.com/0xJacky/Nginx-UI/settings"
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pkg/errors"
"gorm.io/gorm"
"net/http" "net/http"
"net/url" "net/url"
) )
type CasdoorLoginUser struct {
Code string `json:"code" binding:"required,max=255"`
State string `json:"state" binding:"required,max=255"`
}
func CasdoorCallback(c *gin.Context) {
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 {
api.ErrHandler(c, err)
return
}
claims, err := casdoorsdk.ParseJwtToken(token.AccessToken)
if err != nil {
api.ErrHandler(c, err)
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 {
api.ErrHandler(c, err)
}
return
}
userToken, err := model.GenerateJWT(u.Name)
if err != nil {
api.ErrHandler(c, err)
return
}
c.JSON(http.StatusOK, LoginResponse{
Message: "ok",
Token: userToken,
})
}
func GetCasdoorUri(c *gin.Context) { func GetCasdoorUri(c *gin.Context) {
endpoint := settings.CasdoorSettings.Endpoint endpoint := settings.CasdoorSettings.Endpoint
clientId := settings.CasdoorSettings.ClientId clientId := settings.CasdoorSettings.ClientId
redirectUri := settings.CasdoorSettings.RedirectUri redirectUri := settings.CasdoorSettings.RedirectUri
state := settings.CasdoorSettings.Application state := settings.CasdoorSettings.Application
if endpoint == "" || clientId == "" || redirectUri == "" || state == "" { if endpoint == "" || clientId == "" || redirectUri == "" || state == "" {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"uri": "", "uri": "",
}) })
return return
} }
encodedRedirectUri := url.QueryEscape(redirectUri) encodedRedirectUri := url.QueryEscape(redirectUri)
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"uri": fmt.Sprintf("%s/login/oauth/authorize?client_id=%s&response_type=code&redirect_uri=%s&state=%s&scope=read", endpoint, clientId, encodedRedirectUri, state), "uri": fmt.Sprintf("%s/login/oauth/authorize?client_id=%s&response_type=code&redirect_uri=%s&state=%s&scope=read", endpoint, clientId, encodedRedirectUri, state),
}) })

925
app/pnpm-lock.yaml generated

File diff suppressed because it is too large Load diff

View file

@ -4,6 +4,7 @@ import { useUserStore } from '@/pinia'
const { login, logout } = useUserStore() const { login, logout } = useUserStore()
export interface AuthResponse { export interface AuthResponse {
message: string
token: string token: string
} }
@ -25,11 +26,14 @@ const auth = {
login(r.token) login(r.token)
}) })
}, },
logout() { async logout() {
return http.delete('/logout').then(async () => { return http.delete('/logout').then(async () => {
logout() logout()
}) })
}, },
async get_casdoor_uri(): Promise<{ uri: string }> {
return http.get('/casdoor_uri')
},
} }
export default auth export default auth

View file

@ -1 +1 @@
{"version":"2.0.0-beta.4","build_id":68,"total_build":272} {"version":"2.0.0-beta.4","build_id":72,"total_build":276}

View file

@ -6,7 +6,6 @@ import { useUserStore } from '@/pinia'
import auth from '@/api/auth' import auth from '@/api/auth'
import install from '@/api/install' import install from '@/api/install'
import SetLanguage from '@/components/SetLanguage/SetLanguage.vue' import SetLanguage from '@/components/SetLanguage/SetLanguage.vue'
import http from '@/lib/http'
import SwitchAppearance from '@/components/SwitchAppearance/SwitchAppearance.vue' import SwitchAppearance from '@/components/SwitchAppearance/SwitchAppearance.vue'
const thisYear = new Date().getFullYear() const thisYear = new Date().getFullYear()
@ -77,11 +76,11 @@ watch(() => gettext.current, () => {
const has_casdoor = ref(false) const has_casdoor = ref(false)
const casdoor_uri = ref('') const casdoor_uri = ref('')
http.get('/casdoor_uri') auth.get_casdoor_uri()
.then(response => { .then(r => {
if (response?.uri) { if (r?.uri) {
has_casdoor.value = true has_casdoor.value = true
casdoor_uri.value = response.uri casdoor_uri.value = r.uri
} }
}) })
.catch(e => { .catch(e => {

View file

@ -1 +1 @@
{"version":"2.0.0-beta.4","build_id":68,"total_build":272} {"version":"2.0.0-beta.4","build_id":72,"total_build":276}