-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SDK-2370: Added support for Advanced Identity Profiles Requirements f… (
#302) * SDK-2370: Added support for Advanced Identity Profiles Requirements for Share V2 * SDK-2370: renamed file and fixed spelling --------- Co-authored-by: System Administrator <>
- Loading branch information
1 parent
c308aa9
commit 7dc79f0
Showing
12 changed files
with
470 additions
and
590 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,7 @@ report.json | |
# idea files | ||
.idea | ||
|
||
# DS_Store files | ||
.DS_Store | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/getyoti/yoti-go-sdk/v3/digitalidentity" | ||
) | ||
|
||
var advancedIdentityProfile = []byte(`{ | ||
"profiles": [ | ||
{ | ||
"trust_framework": "YOTI_GLOBAL", | ||
"schemes": [ | ||
{ | ||
"label": "LB321", | ||
"type": "IDENTITY", | ||
"objective": "AL_L1" | ||
} | ||
] | ||
} | ||
] | ||
}`) | ||
|
||
func buildAdvancedIdentitySessionReq() (sessionSpec *digitalidentity.ShareSessionRequest, err error) { | ||
policy, err := (&digitalidentity.PolicyBuilder{}).WithAdvancedIdentityProfileRequirements(advancedIdentityProfile).Build() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to build Advanced Identity Requirements policy: %v", err) | ||
} | ||
|
||
subject := []byte(`{ | ||
"subject_id": "unique-user-id-for-examples" | ||
}`) | ||
|
||
sessionReq, err := (&digitalidentity.ShareSessionRequestBuilder{}).WithPolicy(policy).WithRedirectUri("https://localhost:8080/v2/receipt-info").WithSubject(subject).Build() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to build create session request: %v", err) | ||
} | ||
return &sessionReq, nil | ||
} | ||
|
||
func generateAdvancedIdentitySession(w http.ResponseWriter, r *http.Request) { | ||
didClient, err := initialiseDigitalIdentityClient() | ||
if err != nil { | ||
fmt.Fprintf(w, "Client could't be generated: %v", err) | ||
return | ||
} | ||
|
||
sessionReq, err := buildAdvancedIdentitySessionReq() | ||
if err != nil { | ||
fmt.Fprintf(w, "failed to build session request: %v", err) | ||
return | ||
} | ||
|
||
shareSession, err := didClient.CreateShareSession(sessionReq) | ||
if err != nil { | ||
fmt.Fprintf(w, "failed to create share session: %v", err) | ||
return | ||
} | ||
|
||
output, err := json.Marshal(shareSession) | ||
if err != nil { | ||
fmt.Fprintf(w, "failed to marshall share session: %v", err) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprintf(w, string(output)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"html/template" | ||
"image" | ||
"image/jpeg" | ||
"io" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func receipt(w http.ResponseWriter, r *http.Request) { | ||
didClient, err := initialiseDigitalIdentityClient() | ||
if err != nil { | ||
fmt.Fprintf(w, "Client could't be generated") | ||
return | ||
} | ||
receiptID := r.URL.Query().Get("ReceiptID") | ||
|
||
receiptValue, err := didClient.GetShareReceipt(receiptID) | ||
if err != nil { | ||
fmt.Fprintf(w, "failed to get share receipt: %v", err) | ||
return | ||
} | ||
|
||
userProfile := receiptValue.UserContent.UserProfile | ||
|
||
selfie := userProfile.Selfie() | ||
var base64URL string | ||
if selfie != nil { | ||
base64URL = selfie.Value().Base64URL() | ||
|
||
decodedImage := decodeImage(selfie.Value().Data()) | ||
file := createImage() | ||
saveImage(decodedImage, file) | ||
} | ||
|
||
dob, err := userProfile.DateOfBirth() | ||
if err != nil { | ||
errorPage(w, r.WithContext(context.WithValue( | ||
r.Context(), | ||
contextKey("yotiError"), | ||
fmt.Sprintf("Error parsing Date of Birth attribute. Error %q", err.Error()), | ||
))) | ||
return | ||
} | ||
|
||
var dateOfBirthString string | ||
if dob != nil { | ||
dateOfBirthString = dob.Value().String() | ||
} | ||
|
||
templateVars := map[string]interface{}{ | ||
"profile": userProfile, | ||
"selfieBase64URL": template.URL(base64URL), | ||
"rememberMeID": receiptValue.RememberMeID, | ||
"dateOfBirth": dateOfBirthString, | ||
} | ||
|
||
var t *template.Template | ||
t, err = template.New("receipt.html"). | ||
Funcs(template.FuncMap{ | ||
"escapeURL": func(s string) template.URL { | ||
return template.URL(s) | ||
}, | ||
"marshalAttribute": func(name string, icon string, property interface{}, prevalue string) interface{} { | ||
return struct { | ||
Name string | ||
Icon string | ||
Prop interface{} | ||
Prevalue string | ||
}{ | ||
name, | ||
icon, | ||
property, | ||
prevalue, | ||
} | ||
}, | ||
"jsonMarshalIndent": func(data interface{}) string { | ||
json, err := json.MarshalIndent(data, "", "\t") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
return string(json) | ||
}, | ||
}). | ||
ParseFiles("receipt.html") | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
err = t.Execute(w, templateVars) | ||
|
||
if err != nil { | ||
errorPage(w, r.WithContext(context.WithValue( | ||
r.Context(), | ||
contextKey("yotiError"), | ||
fmt.Sprintf("Error applying the parsed profile template. Error: `%s`", err), | ||
))) | ||
return | ||
} | ||
} | ||
func decodeImage(imageBytes []byte) image.Image { | ||
decodedImage, _, err := image.Decode(bytes.NewReader(imageBytes)) | ||
|
||
if err != nil { | ||
panic("Error when decoding the image: " + err.Error()) | ||
} | ||
|
||
return decodedImage | ||
} | ||
|
||
func createImage() (file *os.File) { | ||
file, err := os.Create("./images/YotiSelfie.jpeg") | ||
|
||
if err != nil { | ||
panic("Error when creating the image: " + err.Error()) | ||
} | ||
return | ||
} | ||
|
||
func saveImage(img image.Image, file io.Writer) { | ||
var opt jpeg.Options | ||
opt.Quality = 100 | ||
|
||
err := jpeg.Encode(file, img, &opt) | ||
|
||
if err != nil { | ||
panic("Error when saving the image: " + err.Error()) | ||
} | ||
} |
Oops, something went wrong.