Skip to content
Snippets Groups Projects
client.go 2.33 KiB
Newer Older
  • Learn to ignore specific revisions
  • Yordan Kinkov's avatar
    Yordan Kinkov committed
    package ocm
    
    import (
    	"context"
    	"encoding/json"
    	"fmt"
    	"io"
    	"net/http"
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	"net/url"
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	"strings"
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    )
    
    const (
    	proofOutOfBandPath    = "/proof/v1/out-of-band-proof"
    	proofPresentationPath = "/proof/v1/find-by-presentation-id"
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    )
    
    // Client is the OCM service client
    type Client struct {
    	addr       string
    	httpClient *http.Client
    }
    
    // New initializes an OCM service client given the OCM service address
    func New(addr string, opts ...Option) *Client {
    	c := &Client{
    		addr:       addr,
    		httpClient: http.DefaultClient,
    	}
    
    	for _, opt := range opts {
    		opt(c)
    	}
    
    	return c
    }
    
    // GetLoginProofInvitation calls the "invitation" endpoint on
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    // the "out-of-band" protocol in the OCM.
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    func (c *Client) GetLoginProofInvitation(ctx context.Context, credTypes []string) (*LoginProofInvitationResponse, error) {
    	req, err := http.NewRequestWithContext(ctx, "POST", c.addr+proofOutOfBandPath, nil)
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	if err != nil {
    		return nil, err
    	}
    
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	v := url.Values{}
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	v.Add("type", strings.Join(credTypes, ","))
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	req.URL.RawQuery = v.Encode()
    
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	resp, err := c.httpClient.Do(req)
    	if err != nil {
    		return nil, err
    	}
    	defer resp.Body.Close() // nolint:errcheck
    
    	if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    		return nil, fmt.Errorf("unexpected response code: %s", resp.Status)
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	}
    
    	bytes, err := io.ReadAll(resp.Body)
    	if err != nil {
    		return nil, err
    	}
    
    	var response LoginProofInvitationResponse
    	if err := json.Unmarshal(bytes, &response); err != nil {
    		return nil, err
    	}
    
    	return &response, nil
    }
    
    // GetLoginProofResult calls the "find-by-presentation-id" endpoint in the OCM.
    func (c *Client) GetLoginProofResult(ctx context.Context, presentationID string) (*LoginProofResultResponse, error) {
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	req, err := http.NewRequestWithContext(ctx, "GET", c.addr+proofPresentationPath, nil)
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	if err != nil {
    		return nil, err
    	}
    
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	v := url.Values{}
    	v.Add("presentationId", presentationID)
    	req.URL.RawQuery = v.Encode()
    
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	resp, err := c.httpClient.Do(req)
    	if err != nil {
    		return nil, err
    	}
    	defer resp.Body.Close() // nolint:errcheck
    
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	if resp.StatusCode != http.StatusOK {
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    		return nil, fmt.Errorf("unexpected response code: %s", resp.Status)
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	}
    
    	bytes, err := io.ReadAll(resp.Body)
    	if err != nil {
    		return nil, err
    	}
    
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	var response LoginProofResultResponse
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	if err := json.Unmarshal(bytes, &response); err != nil {
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    		return nil, err
    	}
    
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    	return &response, nil
    
    Yordan Kinkov's avatar
    Yordan Kinkov committed
    }