Skip to content

Commit

Permalink
Merge pull request #83 from mbethke/auth
Browse files Browse the repository at this point in the history
Add session data to ClientConfig
  • Loading branch information
stmcginnis authored Sep 23, 2020
2 parents cb247f3 + 8bfb2ff commit 42c14ec
Showing 1 changed file with 50 additions and 20 deletions.
70 changes: 50 additions & 20 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ type APIClient struct {
dumpWriter io.Writer
}

// Session holds the session ID and auth token needed to identify an
// authenticated client
type Session struct {
ID string
Token string
}

// ClientConfig holds the settings for establishing a connection.
type ClientConfig struct {
// Endpoint is the URL of the redfish service
Expand All @@ -55,6 +62,10 @@ type ClientConfig struct {
// Password is the password to use for authentication.
Password string

// Session is an optional session ID+token obtained from a previous session
// If this is set, it is preferred over Username and Password
Session *Session

// Insecure controls whether to enforce SSL certificate validity.
Insecure bool

Expand Down Expand Up @@ -106,29 +117,36 @@ func Connect(config ClientConfig) (c *APIClient, err error) {
client.HTTPClient = config.HTTPClient
}

if config.Username != "" {
// Authenticate with the service
service, err := ServiceRoot(client)
if err != nil {
return nil, err
}
// Authenticate with the service
service, err := ServiceRoot(client)
if err != nil {
return nil, err
}
client.Service = service

var auth *redfish.AuthToken
if config.BasicAuth {
auth = &redfish.AuthToken{
Username: config.Username,
Password: config.Password,
BasicAuth: true,
}
} else {
auth, err = service.CreateSession(config.Username, config.Password)
if err != nil {
return nil, err
}
if config.Session != nil {
client.auth = &redfish.AuthToken{
Session: config.Session.ID,
Token: config.Session.Token,
}
} else {
if config.Username != "" {
var auth *redfish.AuthToken
if config.BasicAuth {
auth = &redfish.AuthToken{
Username: config.Username,
Password: config.Password,
BasicAuth: true,
}
} else {
auth, err = service.CreateSession(config.Username, config.Password)
if err != nil {
return nil, err
}
}

client.Service = service
client.auth = auth
client.auth = auth
}
}

return client, err
Expand All @@ -153,6 +171,18 @@ func ConnectDefault(endpoint string) (c *APIClient, err error) {
return client, err
}

// GetSession retrieves the session data from an initialized APIClient. An error
// is returned if the client is not authenticated.
func (c *APIClient) GetSession() (*Session, error) {
if c.auth.Session == "" {
return nil, fmt.Errorf("client not authenticated")
}
return &Session{
ID: c.auth.Session,
Token: c.auth.Token,
}, nil
}

// Get performs a GET request against the Redfish service.
func (c *APIClient) Get(url string) (*http.Response, error) {
relativePath := url
Expand Down

0 comments on commit 42c14ec

Please sign in to comment.