Newer
Older
proofOutOfBandPath = "/v1/out-of-band-proof"
proofOutOfBandRequestPath = "/v1/send-out-of-band-presentation-request"
proofPresentationPath = "/v1/find-by-presentation-id"
)
// Client is the OCM service client
type Client struct {
proofManagerAddr string
httpClient *http.Client
}
// New initializes an OCM service client given the OCM service address
func New(proofManagerAddr string, opts ...Option) *Client {
proofManagerAddr: proofManagerAddr,
httpClient: http.DefaultClient,
}
for _, opt := range opts {
opt(c)
}
return c
}
// GetLoginProofInvitation calls the "invitation" endpoint on
func (c *Client) GetLoginProofInvitation(ctx context.Context, credTypes []string) (*LoginProofInvitationResponse, error) {
req, err := http.NewRequestWithContext(ctx, "POST", c.proofManagerAddr+proofOutOfBandPath, nil)
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 {
return nil, fmt.Errorf("unexpected response code: %s", resp.Status)
}
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
}
// SendOutOfBandRequest calls the "send out of band presentation request" endpoint on
// the "out-of-band" protocol in the OCM.
func (c *Client) SendOutOfBandRequest(ctx context.Context, r map[string]interface{}) (*LoginProofInvitationResponse, error) {
body, err := json.Marshal(r)
req, err := http.NewRequestWithContext(ctx, "POST", c.proofManagerAddr+proofOutOfBandRequestPath, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
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 {
return nil, fmt.Errorf("unexpected response code: %s", resp.Status)
}
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response LoginProofInvitationResponse
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// GetLoginProofResult calls the "find-by-presentation-id" endpoint in the OCM.
func (c *Client) GetLoginProofResult(ctx context.Context, presentationID string) (*LoginProofResultResponse, error) {
resBytes, err := c.findByPresentationID(ctx, presentationID)
if err != nil {
return nil, err
}
var response LoginProofResultResponse
if err := json.Unmarshal(resBytes, &response); err != nil {
return nil, err
}
return &response, nil
}
// GetRawLoginProofResult calls the "find-by-presentation-id" endpoint in the OCM and returns the raw result.
func (c *Client) GetRawLoginProofResult(ctx context.Context, presentationID string) (map[string]interface{}, error) {
resBytes, err := c.findByPresentationID(ctx, presentationID)
if err != nil {
return nil, err
}
var response map[string]interface{}
if err := json.Unmarshal(resBytes, &response); err != nil {
return nil, err
}
return response, nil
}
func (c *Client) findByPresentationID(ctx context.Context, presentationID string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, "GET", c.proofManagerAddr+proofPresentationPath, nil)
if err != nil {
return nil, err
}
v := url.Values{}
v.Add("presentationId", presentationID)
req.URL.RawQuery = v.Encode()
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close() // nolint:errcheck
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected response code: %s", resp.Status)
}
return io.ReadAll(resp.Body)
}