mirror of
https://github.com/KaySar12/NextZen-UserService.git
synced 2025-10-06 19:59:42 +07:00
update
This commit is contained in:
+40
-3
@@ -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{}{
|
||||
|
||||
Reference in New Issue
Block a user