diff --git a/digital_identity_client_test.go b/digital_identity_client_test.go index 12d88dc5..f5deaff1 100644 --- a/digital_identity_client_test.go +++ b/digital_identity_client_test.go @@ -31,7 +31,7 @@ func TestDigitalIDClient_KeyLoad_Failure(t *testing.T) { assert.Check(t, !temporary || !tempError.Temporary()) } -func TestDigitalIDClient_CreateShareURL(t *testing.T) { +func TestDigitalIDClient_CreateShare(t *testing.T) { policy, err := (&digitalidentity.PolicyBuilder{}).WithFullName().WithWantedRememberMe().Build() assert.NilError(t, err) @@ -39,5 +39,4 @@ func TestDigitalIDClient_CreateShareURL(t *testing.T) { session, err := (&digitalidentity.ShareSessionBuilder{}).WithPolicy(policy).Build() assert.NilError(t, err) fmt.Println(session) - } diff --git a/digitalidentity/service.go b/digitalidentity/service.go index 9e4afb66..1d7b71cd 100644 --- a/digitalidentity/service.go +++ b/digitalidentity/service.go @@ -3,6 +3,7 @@ package digitalidentity import ( "crypto/rsa" "encoding/json" + "fmt" "io" "net/http" @@ -47,9 +48,8 @@ func CreateShareSession(httpClient requests.HttpClient, shareSession *ShareSessi return share, err } - response, err := requests.Execute(httpClient, request, ShareURLHTTPErrorMessages, yotierror.DefaultHTTPErrorMessages) + response, err := requests.Execute(httpClient, request, ShareHTTPErrorMessages, yotierror.DefaultHTTPErrorMessages) if err != nil { - //fmt.Printf("err 2:=> %s\n\r", err) return share, err } @@ -67,7 +67,7 @@ func CreateShareSession(httpClient requests.HttpClient, shareSession *ShareSessi // GetSession get session info using the supplied sessionID func GetSession(httpClient requests.HttpClient, sessionID string, clientSdkId, apiUrl string, key *rsa.PrivateKey) (share ShareSessionResult, err error) { - endpoint := identitySesssionCreationEndpoint + endpoint := fmt.Sprintf(identitySessionRetrieval, sessionID) headers := requests.AuthHeader(clientSdkId) request, err := requests.SignedRequest{ Key: key, @@ -80,7 +80,7 @@ func GetSession(httpClient requests.HttpClient, sessionID string, clientSdkId, a return share, err } - response, err := requests.Execute(httpClient, request, ShareURLHTTPErrorMessages, yotierror.DefaultHTTPErrorMessages) + response, err := requests.Execute(httpClient, request, ShareHTTPErrorMessages, yotierror.DefaultHTTPErrorMessages) if err != nil { return share, err } @@ -96,7 +96,7 @@ func GetSession(httpClient requests.HttpClient, sessionID string, clientSdkId, a return share, err } -// GetSession get session info using the supplied sessionID +// Create Share QR using the supplied sessionID func CreateShareQrCode(httpClient requests.HttpClient, sessionID string, clientSdkId, apiUrl string, key *rsa.PrivateKey) (qrCode CreateShareQrCodeResult, err error) { endpoint := identitySessionQrCodeCreation headers := requests.AuthHeader(clientSdkId) @@ -112,7 +112,7 @@ func CreateShareQrCode(httpClient requests.HttpClient, sessionID string, clientS return qrCode, err } - response, err := requests.Execute(httpClient, request, ShareURLHTTPErrorMessages, yotierror.DefaultHTTPErrorMessages) + response, err := requests.Execute(httpClient, request, ShareHTTPErrorMessages, yotierror.DefaultHTTPErrorMessages) if err != nil { return qrCode, err } @@ -127,3 +127,34 @@ func CreateShareQrCode(httpClient requests.HttpClient, sessionID string, clientS return qrCode, err } + +// Get Share QR info using the supplied sessionID +func GetShareQr(httpClient requests.HttpClient, sessionID string, clientSdkId, apiUrl string, key *rsa.PrivateKey) (share ShareSessionFetchedQrCode, err error) { + endpoint := fmt.Sprintf(identitySessionQrCodeRetrieval, sessionID) + headers := requests.AuthHeader(clientSdkId) + request, err := requests.SignedRequest{ + Key: key, + HTTPMethod: http.MethodGet, + BaseURL: apiUrl, + Endpoint: endpoint, + Headers: headers, + }.Request() + if err != nil { + return share, err + } + + response, err := requests.Execute(httpClient, request, ShareHTTPErrorMessages, yotierror.DefaultHTTPErrorMessages) + if err != nil { + return share, err + } + defer response.Body.Close() + + responseBytes, err := io.ReadAll(response.Body) + if err != nil { + return share, err + } + + err = json.Unmarshal(responseBytes, &share) + + return share, err +} diff --git a/digitalidentity/service_test.go b/digitalidentity/service_test.go index 5a7e9a58..25e19b9d 100644 --- a/digitalidentity/service_test.go +++ b/digitalidentity/service_test.go @@ -167,3 +167,30 @@ func TestGetSession(t *testing.T) { assert.Equal(t, "SOME_QRCODE_ID", result.QrCode.Id) assert.Equal(t, "SOME_RECEIPT_ID", result.Receipt.Id) } + +type HttpClientMock struct{} + +func (c *HttpClientMock) Do(req *http.Request) (*http.Response, error) { + recorder := httptest.NewRecorder() + recorder.WriteString(`{"some": "json"}`) + return recorder.Result(), nil +} + +func TestGetShareQr(t *testing.T) { + httpClient := &HttpClientMock{} + sessionID := "testSessionID" + clientSdkId := "testClientSdkId" + apiUrl := "http://test.api" + privateKey := test.GetValidKey("../test/test-key.pem") + + share, err := GetShareQr(httpClient, sessionID, clientSdkId, apiUrl, privateKey) + + if err != nil { + t.Errorf("Expected no error, but got: %v", err) + } + + if &share == nil { + t.Error("Expected a non-nil share, but got nil") + } + +} diff --git a/digitalidentity/share_retrieve_qr.go b/digitalidentity/share_retrieve_qr.go new file mode 100644 index 00000000..fe737d9b --- /dev/null +++ b/digitalidentity/share_retrieve_qr.go @@ -0,0 +1,10 @@ +package digitalidentity + +type ShareSessionFetchedQrCode struct { + ID string `json:"id"` + Expiry string `json:"expiry"` + Policy string `json:"policy"` + Extensions []interface{} `json:"extensions"` + Session ShareSessionCreated `json:"session"` + RedirectURI string `json:"redirectUri"` +} diff --git a/digitalidentity/share_session.go b/digitalidentity/share_session.go index ff5c865c..146ebd5d 100644 --- a/digitalidentity/share_session.go +++ b/digitalidentity/share_session.go @@ -1,14 +1,5 @@ package digitalidentity -var ( - // ShareURLHTTPErrorMessages specifies the HTTP error status codes used - // by the Share URL API - ShareSessionHTTPErrorMessages = map[int]string{ - 400: "JSON is incorrect, contains invalid data", - 404: "Application was not found", - } -) - // ShareSessionResult contains a dynamic share QR code type ShareSessionResult struct { Id string `json:"id"` diff --git a/digitalidentity/share_session_created.go b/digitalidentity/share_session_created.go new file mode 100644 index 00000000..86d60e3f --- /dev/null +++ b/digitalidentity/share_session_created.go @@ -0,0 +1,8 @@ +package digitalidentity + +// ShareSessionCreated Share Session QR Result +type ShareSessionCreated struct { + ID string `json:"id"` + Satus string `json:"status"` + Expiry string `json:"expiry"` +} diff --git a/digitalidentity/share_url.go b/digitalidentity/share_url.go index 57a25e87..e7c48092 100644 --- a/digitalidentity/share_url.go +++ b/digitalidentity/share_url.go @@ -3,7 +3,7 @@ package digitalidentity var ( // ShareURLHTTPErrorMessages specifies the HTTP error status codes used // by the Share URL API - ShareURLHTTPErrorMessages = map[int]string{ + ShareHTTPErrorMessages = map[int]string{ 400: "JSON is incorrect, contains invalid data", 404: "Application was not found", }