package passwordsafe import ( "fmt" "github.com/sirupsen/logrus" raxidentity "github.rackspace.com/gorax/clients/raxIdentity" "github.rackspace.com/gorax/webutil" ) const ( pwSafeURL = "https://passwordsafe.corp.rackspace.com/" ) type ( Client struct { apikey *string } ) // NewTokenClient creates a client based on an X-Auth-Token func NewTokenClient(token string) (*Client, error) { return testAuth(token) } // NewClient creates a client based on username and password func NewClient(user, password string) (*Client, error) { auth, err := raxidentity.InternalAuth(user, password, false) if err != nil { return nil, err } return testAuth(auth.APIKey()) } // testAuth tests if the token works func testAuth(token string) (*Client, error) { _, err := get(pwSafeURL+"projects.json", token) if err != nil { logrus.WithField("error", err.Error()) return nil, err } return &Client{ apikey: &token, }, nil } func (p *Client) get(path string) (*[]byte, error) { return get(path, *p.apikey) } func get(path, token string) (*[]byte, error) { res, code, err := webutil.JSONGet(path, map[string]string{"X-Auth-Token": token}, false) if err != nil { return nil, err } if code != 200 { return nil, fmt.Errorf("unexpected response code %d", code) } return &res, nil }