This commit is contained in:
KaySar12 2024-07-05 18:35:54 +07:00
parent 219fa12590
commit 465e77832e
4 changed files with 54 additions and 30 deletions

2
dist/metadata.json vendored
View File

@ -1 +1 @@
{"project_name":"casaos-user-service","tag":"v1.0.0","previous_tag":"","version":"1.0.1","commit":"dd3a9ebea4fd121d6a2383881265ed85147dbeca","date":"2024-07-04T10:29:51.218513943+07:00","runtime":{"goos":"linux","goarch":"amd64"}}
{"project_name":"casaos-user-service","tag":"v1.0.0","previous_tag":"","version":"1.0.1","commit":"219fa12590c7ebbe57526514ffea6ac2b2e63641","date":"2024-07-05T17:28:10.805445951+07:00","runtime":{"goos":"linux","goarch":"amd64"}}

View File

@ -51,26 +51,13 @@ type OMVLogin struct {
} `json:"response"`
Error interface{} `json:"error"`
}
type OMVGetUser struct {
type OMVUser struct {
Response struct {
Name string `json:"name"`
UID int `json:"uid"`
Gid int `json:"gid"`
Comment string `json:"comment"`
Dir string `json:"dir"`
Shell string `json:"shell"`
Lastchanged string `json:"lastchanged"`
Minimum string `json:"minimum"`
Maximum string `json:"maximum"`
Warn string `json:"warn"`
Inactive string `json:"inactive"`
Expire string `json:"expire"`
Reserved string `json:"reserved"`
Groups []string `json:"groups"`
System bool `json:"system"`
Email string `json:"email"`
Disallowusermod bool `json:"disallowusermod"`
Sshpubkeys []interface{} `json:"sshpubkeys"`
Authenticated bool `json:"authenticated"`
Username string `json:"username"`
Permissions struct {
Role string `json:"role"`
} `json:"permissions"`
} `json:"response"`
Error interface{} `json:"error"`
}
@ -221,11 +208,9 @@ func PostOMVLogin(c *gin.Context) {
json := make(map[string]string)
c.ShouldBind(&json)
username := json["username"]
password := json["password"]
res := service.MyService.OMV().LoginSession(username, password)
res, cookies := service.MyService.OMV().LoginSession(username, password)
var resData OMVLogin
err := json2.Unmarshal([]byte(res), &resData)
@ -240,14 +225,14 @@ func PostOMVLogin(c *gin.Context) {
return
}
getUser, err := service.MyService.OMV().GetUser(username, resData.Response.SessionID)
getUser, err := service.MyService.OMV().AuthUser(username, password, resData.Response.SessionID)
if err != nil {
// Handle the error, for example, log it or return it
log.Printf("Error getting user: %v", err)
return // or handle it in a way that fits your application's error handling strategy
}
var userData OMVGetUser
var userData OMVUser
err = json2.Unmarshal([]byte(getUser), &userData)
if err != nil {
@ -266,8 +251,10 @@ func PostOMVLogin(c *gin.Context) {
// cookie_value, err := c.Cookie("sessionID")
// decrypt := encryption.Decrypt(cookie_value)
// fmt.Printf(decrypt)
sessionId := encryption.Encrypt(resData.Response.SessionID)
c.SetCookie("sessionID", sessionId, 3600, "/", "", false, true)
// sessionId := encryption.Encrypt(resData.Response.SessionID)
for _, cookie := range cookies {
c.SetCookie(cookie.Name, cookie.Value, 3600, "/", "", false, true)
}
c.JSON(common_err.SUCCESS,
model.Result{
Success: common_err.SUCCESS,

View File

@ -14,16 +14,19 @@ import (
)
type OMVService interface {
LoginSession(userName string, password string) string
LoginSession(userName string, password string) (string, []*http.Cookie)
Logout(sessionID string) (string, error)
GetUser(username string, sessionID string) (string, error)
AuthUser(username string, password string, sessionID string) (string, error)
SetUser(m model.UserDBModel) model.UserDBModel
ApplyChange()
}
type omvService struct {
}
func (o *omvService) LoginSession(username string, password string) string {
// AuthUser implements OMVService.
func (o *omvService) LoginSession(username string, password string) (string, []*http.Cookie) {
postBody, _ := json.Marshal(map[string]interface{}{
"service": "session",
"method": "login",
@ -34,6 +37,7 @@ func (o *omvService) LoginSession(username string, password string) string {
})
responseBody := bytes.NewBuffer(postBody)
response, err := http.Post(config.AppInfo.OMVServer, "application/json", responseBody)
cookies := response.Cookies()
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
@ -42,7 +46,7 @@ func (o *omvService) LoginSession(username string, password string) string {
if err != nil {
log.Fatal(err)
}
return string(responseData)
return string(responseData), cookies
}
func (o *omvService) Logout(sessionID string) (string, error) {
postBody, _ := json.Marshal(map[string]interface{}{
@ -75,7 +79,40 @@ func (o *omvService) Logout(sessionID string) (string, error) {
return string(responseData), nil
}
func (o *omvService) AuthUser(username string, password string, sessionID string) (string, error) {
postBody, _ := json.Marshal(map[string]interface{}{
"service": "session",
"method": "login",
"params": map[string]string{
"username": username,
"password": password,
},
})
responseBody := bytes.NewBuffer(postBody)
req, err := http.NewRequest("POST", config.AppInfo.OMVServer, responseBody)
if err != nil {
return "", fmt.Errorf("error creating request: %v", err)
}
req.Header.Set("X-OPENMEDIAVAULT-SESSIONID", sessionID) // Set session ID header
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("error making request: %v", err)
}
defer resp.Body.Close()
// Check for HTTP errors
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("HTTP error: %s", resp.Status)
}
// Read the response body
responseData, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response body: %v", err)
}
return string(responseData), nil
}
func (o *omvService) GetUser(username string, sessionID string) (string, error) {
// Prepare the RPC request
postBody, _ := json.Marshal(map[string]interface{}{