v1.0.5
This commit is contained in:
1 parent
d340d1b2ad
commit
7ccd84cd96
101 files changed
+2070
-344
No files matched your search
@@ -0,0 +1,71 @@
|
||||
// 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
|
||||
}
|
||||
Reference in new issue
Block a user