Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
golib
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gaia-X
Trust Services API
golib
Commits
810b020c
Commit
810b020c
authored
2 years ago
by
Yordan Kinkov
Browse files
Options
Downloads
Plain Diff
Merge branch '3-modify-ocm-client' into 'main'
Modify ocm client Closes
#3
See merge request
!12
parents
66d0023c
aaa33d22
No related branches found
Branches containing commit
Tags
v1.1.0
Tags containing commit
1 merge request
!12
Modify ocm client
Pipeline
#52886
passed with stage
in 42 seconds
Changes
3
Pipelines
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
ocm/client.go
+49
-9
49 additions, 9 deletions
ocm/client.go
ocm/client_test.go
+35
-29
35 additions, 29 deletions
ocm/client_test.go
ocm/types.go
+13
-13
13 additions, 13 deletions
ocm/types.go
with
97 additions
and
51 deletions
ocm/client.go
+
49
−
9
View file @
810b020c
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
.
Status
Code
)
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
*
LoginProof
Invitation
Response
var
response
LoginProof
Result
Response
if
err
:=
json
.
Unmarshal
(
bytes
,
&
response
);
err
!=
nil
{
return
nil
,
err
}
return
response
,
nil
return
&
response
,
nil
}
This diff is collapsed.
Click to expand it.
ocm/client_test.go
+
35
−
29
View file @
810b020c
...
...
@@ -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
.
GetLoginProof
Invitation
(
context
.
Background
(),
req
)
res
,
err
:=
client
.
GetLoginProof
Result
(
context
.
Background
(),
"2cf01406-b15f-4960-a6a7-7bc62cd37a3c"
)
assert
.
Nil
(
t
,
res
)
assert
.
Error
(
t
,
err
)
...
...
This diff is collapsed.
Click to expand it.
ocm/types.go
+
13
−
13
View file @
810b020c
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"`
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment