package passwordsafe import ( "encoding/json" "errors" "fmt" "strings" "time" ) type ( Credential struct { Credential ProjectCredential `json:"credential"` } ProjectCredential struct { Category string `json:"category"` Created time.Time `json:"created_at"` Description string `json:"description"` HostName string `json:"hostname"` Id int `json:"id"` Password string `json:"password"` Prereqs string `json:"prerequisites"` ProjectId int `json:"project_id"` Updated time.Time `json:"updated_at"` Url string `json:"url"` UserId int `json:"user_id"` Username string `json:"username"` Version int `json:"version"` } ) // ProjectCredentials returns all credentials store in a project in password safe func (p *Client) ProjectCredentials(pr *Project) ([]Credential, error) { res, err := p.get(fmt.Sprintf("%sprojects/%d/credentials.json?page=0", pwSafeURL, pr.Project.Id)) if err != nil { return nil, err } var cl []Credential if err := json.Unmarshal(*res, &cl); err != nil { return nil, err } return cl, nil } // Credentials will take a project number and credential description and return the username and password of the first credential that matches func (p *Client) Credentials(project int, desc string) (string, string, error) { var username, password string d := ProjectData{ Id: project, Description: desc, } projCreds, err := p.ProjectCredentials(&Project{Project: d}) if err != nil { return "", "", err } for _, cred := range projCreds { if strings.ToLower(cred.Credential.Description) == strings.ToLower(desc) { username = cred.Credential.Username password = cred.Credential.Password break } } if username == "" && password == "" { return "", "", errors.New("Unable to find credentials in given project") } return username, password, nil }