Skip to content
Snippets Groups Projects
Commit 810b020c authored by Yordan Kinkov's avatar Yordan Kinkov
Browse files

Merge branch '3-modify-ocm-client' into 'main'

Modify ocm client

Closes #3

See merge request gaiax/tsa/golib!12
parents 66d0023c aaa33d22
No related branches found
Tags v1.1.0
No related merge requests found
Pipeline #21371 canceled with stage
package ocm
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
const (
proofOutOfBandPath = "/proof/v1/out-of-band-proof"
proofPresentationPath = "/proof/v1/find-by-presentation-id"
)
// Client is the OCM service client
......@@ -30,26 +35,61 @@ func New(addr string, opts ...Option) *Client {
}
// GetLoginProofInvitation calls the "invitation" endpoint on
// the "out-of-band" protocol in the OCM service.
func (c *Client) GetLoginProofInvitation(ctx context.Context, r *LoginProofInvitationRequest) (*LoginProofInvitationResponse, error) {
b, err := json.Marshal(r)
// the "out-of-band" protocol in the OCM.
func (c *Client) GetLoginProofInvitation(ctx context.Context, credTypes []string) (*LoginProofInvitationResponse, error) {
req, err := http.NewRequestWithContext(ctx, "POST", c.addr+proofOutOfBandPath, nil)
if err != nil {
return nil, err
}
v := url.Values{}
for _, t := range credTypes {
v.Add("type", t)
}
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.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
}
req, err := http.NewRequestWithContext(ctx, "POST", c.addr+"/out-of-band/1.0/invitation", bytes.NewReader(b))
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) {
req, err := http.NewRequestWithContext(ctx, "GET", c.addr+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.StatusCreated && resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected response code: %d", resp.StatusCode)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected response code: %s", resp.Status)
}
bytes, err := io.ReadAll(resp.Body)
......@@ -57,10 +97,10 @@ func (c *Client) GetLoginProofInvitation(ctx context.Context, r *LoginProofInvit
return nil, err
}
var response *LoginProofInvitationResponse
var response LoginProofResultResponse
if err := json.Unmarshal(bytes, &response); err != nil {
return nil, err
}
return response, nil
return &response, nil
}
......@@ -3,7 +3,6 @@ package ocm_test
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
......@@ -17,29 +16,16 @@ func Test_GetLoginProofInvitationSuccess(t *testing.T) {
expected := &ocm.LoginProofInvitationResponse{
StatusCode: 200,
Message: "success",
Data: ocm.LoginProofInvitationResponseData{},
}
ocmServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expectedRequestBody := `{"comment":"test","attributes":null,"schemaId":"schema:1.0","participantId":"12345"}`
bodyBytes, err := io.ReadAll(r.Body)
assert.NoError(t, err)
bodyString := string(bodyBytes)
assert.Equal(t, expectedRequestBody, bodyString)
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(expected)
}))
req := &ocm.LoginProofInvitationRequest{
Comment: "test",
Attributes: nil,
SchemaID: "schema:1.0",
ParticipantID: "12345",
}
client := ocm.New(ocmServer.URL)
res, err := client.GetLoginProofInvitation(context.Background(), req)
res, err := client.GetLoginProofInvitation(context.Background(), []string{"principalMembershipCredential"})
assert.NoError(t, err)
assert.Equal(t, expected.StatusCode, res.StatusCode)
......@@ -49,25 +35,45 @@ func Test_GetLoginProofInvitationSuccess(t *testing.T) {
func Test_GetLoginProofInvitationErr(t *testing.T) {
ocmServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expectedRequestBody := `{"comment":"test","attributes":null,"schemaId":"schema:1.0","participantId":"12345"}`
bodyBytes, err := io.ReadAll(r.Body)
assert.NoError(t, err)
bodyString := string(bodyBytes)
assert.Equal(t, expectedRequestBody, bodyString)
w.WriteHeader(http.StatusInternalServerError)
}))
req := &ocm.LoginProofInvitationRequest{
Comment: "test",
Attributes: nil,
SchemaID: "schema:1.0",
ParticipantID: "12345",
client := ocm.New(ocmServer.URL)
res, err := client.GetLoginProofInvitation(context.Background(), []string{"principalMembershipCredential"})
assert.Nil(t, res)
assert.Error(t, err)
assert.Contains(t, err.Error(), "unexpected response code")
}
func TestClient_GetLoginProofResultSuccess(t *testing.T) {
expected := &ocm.LoginProofResultResponse{
StatusCode: 200,
Message: "success",
Data: ocm.LoginProofResultResponseData{},
}
ocmServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(expected)
}))
client := ocm.New(ocmServer.URL)
res, err := client.GetLoginProofResult(context.Background(), "2cf01406-b15f-4960-a6a7-7bc62cd37a3c")
assert.NoError(t, err)
assert.Equal(t, expected.StatusCode, res.StatusCode)
assert.Equal(t, expected.Message, res.Message)
assert.Equal(t, expected.Data, res.Data)
}
func Test_GetLoginProofResultErr(t *testing.T) {
ocmServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
client := ocm.New(ocmServer.URL)
res, err := client.GetLoginProofInvitation(context.Background(), req)
res, err := client.GetLoginProofResult(context.Background(), "2cf01406-b15f-4960-a6a7-7bc62cd37a3c")
assert.Nil(t, res)
assert.Error(t, err)
......
package ocm
type LoginProofInvitationRequest struct {
Comment string `json:"comment"`
Attributes []Attribute `json:"attributes"`
SchemaID string `json:"schemaId"`
ParticipantID string `json:"participantId"`
}
type Attribute struct {
AttributeName string `json:"attributeName"`
Value string `json:"value"`
Condition string `json:"condition"`
}
type LoginProofInvitationResponse struct {
StatusCode int `json:"statusCode"`
Message string `json:"message"`
......@@ -23,3 +10,16 @@ type LoginProofInvitationResponseData struct {
PresentationID string `json:"presentationId"`
PresentationMessage string `json:"presentationMessage"`
}
type LoginProofResultResponse struct {
StatusCode int `json:"statusCode"`
Message string `json:"message"`
Data LoginProofResultResponseData `json:"data"`
}
type LoginProofResultResponseData struct {
State string `json:"state"`
SchemaID string `json:"schemaId"`
CredDefID string `json:"credDefId"`
Claims map[string]interface{} `json:"credentialSubject"`
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment