...

Source file src/edge-infra.dev/pkg/edge/api/client/auth.go

Documentation: edge-infra.dev/pkg/edge/api/client

     1  package client
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/shurcooL/graphql"
     7  
     8  	"edge-infra.dev/pkg/edge/api/graph/model"
     9  )
    10  
    11  // loginMutationPayload consists of fields that we would like to exist in the session.
    12  type loginMutationPayload struct {
    13  	FullName           string   `json:"fullName"`
    14  	Token              string   `json:"token"`
    15  	Roles              []string `json:"roles"`
    16  	CredentialsExpired bool     `json:"credentialsExpired"`
    17  	SessionTime        float64  `json:"sessionTime"`
    18  	Organization       string   `json:"organization"`
    19  }
    20  
    21  // Login makes an API request to login the user and set a user session.
    22  func (c *EdgeClient) Login(ctx context.Context, req *LoginRequest) (string, error) {
    23  	var loginMutation struct {
    24  		Login loginMutationPayload `json:"login" graphql:"login(username: $username, password: $password, organization: $organization)"`
    25  	}
    26  	if err := c.Mutate(ctx, &loginMutation, map[string]any{
    27  		"username":     graphql.String(req.Username),
    28  		"password":     graphql.String(req.Password),
    29  		"organization": graphql.String(req.Organization),
    30  	}); err != nil {
    31  		return "", err
    32  	}
    33  	return loginMutation.Login.Token, nil
    34  }
    35  
    36  // SessionRefresh makes an API request to refresh the user session.
    37  func (c *EdgeClient) SessionRefresh(ctx context.Context, provider model.AuthProvider) (string, error) {
    38  	var sessionRefreshMutation struct {
    39  		Token string `graphql:"sessionRefresh(provider: $provider)"`
    40  	}
    41  	if err := c.Client.Mutate(ctx, &sessionRefreshMutation, map[string]any{
    42  		"provider": provider,
    43  	}); err != nil {
    44  		return "", err
    45  	}
    46  	return sessionRefreshMutation.Token, nil
    47  }
    48  

View as plain text