Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK-2370: Added support for Advanced Identity Profiles Requirements f… #302

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ report.json
# idea files
.idea

# DS_Store files
.DS_Store


70 changes: 70 additions & 0 deletions _examples/digitalidentity/advanced_identity_profile.go
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))

}
4 changes: 3 additions & 1 deletion _examples/digitalidentity/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ <h3 class="yoti-sponsor-app-header">The Yoti app is free to download and use:</h

async function completionHandler(receivedReceiptId) {
console.log('completion handler:', receivedReceiptId)
const response = await fetch('/v2/receipt-info?ReceiptID=' + encodeURIComponent(receivedReceiptId) , {method: 'GET'})
const url = '/v2/receipt-info?ReceiptID=' + encodeURIComponent(receivedReceiptId);
// Navigate to the Receipt page
window.location.href = url;
}

function onErrorListener(...data) {
Expand Down
27 changes: 3 additions & 24 deletions _examples/digitalidentity/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func buildDigitalIdentitySessionReq() (sessionSpec *digitalidentity.ShareSession
func generateSession(w http.ResponseWriter, r *http.Request) {
didClient, err := initialiseDigitalIdentityClient()
if err != nil {
fmt.Fprintf(w, string("Client could't be generated"))
fmt.Fprintf(w, "Client could't be generated: %v", err)
return
}

Expand All @@ -93,28 +93,6 @@ func generateSession(w http.ResponseWriter, r *http.Request) {

}

func getReceipt(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
}
output, err := json.Marshal(receiptValue)
if err != nil {
fmt.Fprintf(w, "failed to marshal receipt: %v", err)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, string(output))
}

func initialiseDigitalIdentityClient() (*yoti.DigitalIdentityClient, error) {
var err error
sdkID := os.Getenv("YOTI_CLIENT_SDK_ID")
Expand Down Expand Up @@ -147,7 +125,8 @@ func main() {

http.HandleFunc("/", home)
http.HandleFunc("/v2/generate-share", generateSession)
http.HandleFunc("/v2/receipt-info", getReceipt)
http.HandleFunc("/v2/generate-advanced-identity-share", generateAdvancedIdentitySession)
http.HandleFunc("/v2/receipt-info", receipt)

rootdir, err := os.Getwd()
if err != nil {
Expand Down
136 changes: 136 additions & 0 deletions _examples/digitalidentity/receipt.go
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())
}
}
Loading
Loading