1
0
Fork 0
mirror of https://github.com/Luzifer/nginx-sso.git synced 2024-12-21 05:11:17 +00:00
nginx-sso/vendor/github.com/jda/go-crowd/user.go
Knut Ahlers 9b3c895c04
Update dependencies
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2019-04-22 06:44:07 +02:00

64 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()
c.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 := c.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", 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
}