Skip to content

Commit

Permalink
SDK-2259:Added go retrieve features
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmet-yoti committed Aug 9, 2023
1 parent 38848d1 commit c509f38
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 18 deletions.
3 changes: 1 addition & 2 deletions digital_identity_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ 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)

session, err := (&digitalidentity.ShareSessionBuilder{}).WithPolicy(policy).Build()
assert.NilError(t, err)
fmt.Println(session)

}
43 changes: 37 additions & 6 deletions digitalidentity/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package digitalidentity
import (
"crypto/rsa"
"encoding/json"
"fmt"
"io"
"net/http"

Expand Down Expand Up @@ -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
}

Expand All @@ -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,
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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
}
27 changes: 27 additions & 0 deletions digitalidentity/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

}
10 changes: 10 additions & 0 deletions digitalidentity/share_retrieve_qr.go
Original file line number Diff line number Diff line change
@@ -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"`
}
9 changes: 0 additions & 9 deletions digitalidentity/share_session.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand Down
8 changes: 8 additions & 0 deletions digitalidentity/share_session_created.go
Original file line number Diff line number Diff line change
@@ -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"`
}
2 changes: 1 addition & 1 deletion digitalidentity/share_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down

0 comments on commit c509f38

Please sign in to comment.