mirror of
https://github.com/Luzifer/nginx-sso.git
synced 2024-12-21 05:11:17 +00:00
65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
|
package crowd
|
||
|
|
||
|
import (
|
||
|
"encoding/xml"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
)
|
||
|
|
||
|
// User represents a user in Crowd
|
||
|
type User struct {
|
||
|
XMLName struct{} `xml:"user"`
|
||
|
UserName string `xml:"name,attr"`
|
||
|
FirstName string `xml:"first-name"`
|
||
|
LastName string `xml:"last-name"`
|
||
|
DisplayName string `xml:"display-name"`
|
||
|
Email string `xml:"email"`
|
||
|
Active bool `xml:"active"`
|
||
|
Key string `xml:"key"`
|
||
|
}
|
||
|
|
||
|
// GetUser retrieves user information
|
||
|
func (c *Crowd) GetUser(user string) (User, error) {
|
||
|
u := User{}
|
||
|
|
||
|
v := url.Values{}
|
||
|
v.Set("username", user)
|
||
|
url := c.url + "rest/usermanagement/1/user?" + v.Encode()
|
||
|
client := http.Client{Jar: c.cookies}
|
||
|
req, err := http.NewRequest("GET", url, nil)
|
||
|
if err != nil {
|
||
|
return u, err
|
||
|
}
|
||
|
req.SetBasicAuth(c.user, c.passwd)
|
||
|
req.Header.Set("Accept", "application/xml")
|
||
|
req.Header.Set("Content-Type", "application/xml")
|
||
|
resp, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
return u, err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
switch resp.StatusCode {
|
||
|
case 404:
|
||
|
return u, fmt.Errorf("user not found")
|
||
|
case 200:
|
||
|
// fall through switch without returning
|
||
|
default:
|
||
|
return u, fmt.Errorf("request failed: %s\n", resp.Status)
|
||
|
}
|
||
|
|
||
|
body, err := ioutil.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return u, err
|
||
|
}
|
||
|
|
||
|
err = xml.Unmarshal(body, &u)
|
||
|
if err != nil {
|
||
|
return u, err
|
||
|
}
|
||
|
|
||
|
return u, nil
|
||
|
}
|