71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
// Code generated by internal codegen utility; DO NOT EDIT.
|
|||
|
|
|
||
|
|
package desdk
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"repository.basistech.ru/BASIS/de-golang-sdk/tags/session"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (c *Client) IssueToken(ctx context.Context, username, password string) (string, error) {
|
||
|
|
response, err := c.Session.Create(ctx, session.CreateRequest{Username: username, Password: password})
|
||
|
|
if err != nil {
|
||
|
|
return "", fmt.Errorf("create session: %w", err)
|
||
|
|
}
|
||
|
|
if response.Token == "" {
|
||
|
|
return "", fmt.Errorf("create session: response contains no token")
|
||
|
|
}
|
||
|
|
|
||
|
|
return response.Token, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// SetToken configures the bearer token without validating it or checking compatibility.
|
||
|
|
func (c *Client) SetToken(token string) {
|
||
|
|
c.cfg.AddDefaultHeader("Authorization", "Bearer "+token)
|
||
|
|
}
|
||
|
|
|
||
|
|
// AuthenticateWithToken verifies the token and server compatibility before
|
||
|
|
// configuring the token for subsequent requests.
|
||
|
|
//
|
||
|
|
// The verification is skipped when SkipCompatibilityCheck is enabled.
|
||
|
|
func (c *Client) AuthenticateWithToken(ctx context.Context, token string) error {
|
||
|
|
if ctx == nil {
|
||
|
|
return fmt.Errorf("nil context")
|
||
|
|
}
|
||
|
|
if c == nil {
|
||
|
|
return fmt.Errorf("nil client")
|
||
|
|
}
|
||
|
|
if c.cfg == nil {
|
||
|
|
return fmt.Errorf("nil client configuration")
|
||
|
|
}
|
||
|
|
if strings.TrimSpace(token) == "" {
|
||
|
|
return fmt.Errorf("empty token")
|
||
|
|
}
|
||
|
|
|
||
|
|
if !c.cfg.SkipCompatibilityCheck {
|
||
|
|
probeClient := NewClientWithConfig(c.cfg.Clone())
|
||
|
|
probeClient.SetToken(token)
|
||
|
|
if err := probeClient.CheckCompatibility(ctx); err != nil {
|
||
|
|
return fmt.Errorf("check compatibility: %w", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
c.SetToken(token)
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *Client) Authenticate(ctx context.Context, username, password string) (string, error) {
|
||
|
|
token, err := c.IssueToken(ctx, username, password)
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := c.AuthenticateWithToken(ctx, token); err != nil {
|
||
|
|
return "", fmt.Errorf("authenticate with issued token: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return token, nil
|
||
|
|
}
|