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 2265 examples #354

Merged
merged 6 commits into from
Jun 18, 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
14 changes: 14 additions & 0 deletions examples/digitalidentity/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file is a template for defining the environment variables
# Set the application config values here

YOTI_SDK_ID=xxxxxxxxxxxxxxxxxxxxx

# Below is the private key (in .pem format) associated with the Yoti Application you created on Yoti Hub
YOTI_KEY_FILE_PATH=./keys/php-sdk-access-security.pem

# Laravel config:
APP_NAME=yoti.sdk.digitalidentity.demo
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
16 changes: 16 additions & 0 deletions examples/digitalidentity/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/node_modules
/public/hot
/public/storage
/storage/*.key
/vendor
.env
.env.backup
.phpunit.result.cache
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log

*.pem
keys/*.pem
sdk
24 changes: 24 additions & 0 deletions examples/digitalidentity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Digital Identity Example

## Requirements

This example requires [Docker](https://docs.docker.com/)

## Setup

* Create your application in the [Yoti Hub](https://hub.yoti.com) (this requires having a Yoti account)
* Set the application domain of your app to `localhost:4002`
* Do the steps below inside the [examples/digitalidentity](./) folder
* Put `your-application-pem-file.pem` file inside the [keys](keys) folder, as Docker requires the `.pem` file to reside within the same location where it's run from.
* Copy `.env.example` to `.env`
* Open `.env` file and fill in the environment variable `YOTI_SDK_ID`
* Set `YOTI_KEY_FILE_PATH` to `./keys/your-application-pem-file.pem`
* Install dependencies `docker-compose up composer`
* Run the `docker-compose up --build` command
* Visit [https://localhost:4002](https://localhost:4002)
* Run the `docker-compose stop` command to stop the containers.

> To see how to retrieve activity details using the one time use token, refer to the [digitalidentity controller](app/Http/Controllers/IdentityController.php)

## Digital Identity Example
* Visit [/generate-share](https://localhost:4002/generate-share)
saurabh-yoti marked this conversation as resolved.
Show resolved Hide resolved
41 changes: 41 additions & 0 deletions examples/digitalidentity/app/Console/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
}

/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');

require base_path('routes/console.php');
}
}
55 changes: 55 additions & 0 deletions examples/digitalidentity/app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];

/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];

/**
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*
* @throws \Exception
*/
public function report(Throwable $exception)
{
parent::report($exception);
}

/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
return parent::render($request, $exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Log;
use mysql_xdevapi\Exception;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Yoti\DigitalIdentityClient;
use Yoti\Identity\Policy\PolicyBuilder;
use Yoti\Identity\ShareSessionRequestBuilder;
use Yoti\YotiClient;

class IdentityController extends BaseController
{
public function generateSession(DigitalIdentityClient $client)
{
try {

$policy = (new PolicyBuilder())
->withFamilyName()
->withGivenNames()
->withFullName()
->withDateOfBirth()
->withGender()
->withNationality()
->withPhoneNumber()
->withSelfie()
->withEmail()
->withDocumentDetails()
->withDocumentImages()
->build();

$redirectUri = 'https://host/redirect/';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a real path in the example for redirection.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to real url


$shareSessionRequest = (new ShareSessionRequestBuilder())
->withPolicy($policy)
->withRedirectUri($redirectUri)
->build();
$session = $client->createShareSession($shareSessionRequest);
return $session->getId();
}
catch (\Throwable $e) {
Log::error($e->getTraceAsString());
throw new BadRequestHttpException($e->getMessage());
}
}
public function show(DigitalIdentityClient $client)
{
try {
return view('identity', [
'title' => 'Digital Identity Complete Example',
'sdkId' => $client->id
]);
} catch (\Throwable $e) {
Log::error($e->getTraceAsString());
throw new BadRequestHttpException($e->getMessage());
}
}
}
124 changes: 124 additions & 0 deletions examples/digitalidentity/app/Http/Controllers/ReceiptController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace App\Http\Controllers;

use Yoti\DigitalIdentityClient;
use Yoti\YotiClient;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Yoti\Profile\Attribute;
use Yoti\Profile\UserProfile;
use Yoti\Util\Logger;
class ReceiptController extends BaseController
{
public function show(Request $request, DigitalIdentityClient $client, ?LoggerInterface $logger = null)
{
$logger = $logger ?? new Logger();

$logger->warning("Unknown Content Type parsing as a String");
$shareReceipt = $client->fetchShareReceipt($request->query('ReceiptID'));

$profile = $shareReceipt->getProfile();

return view('receipt', [
'fullName' => $profile->getFullName(),
'selfie' => $profile->getSelfie(),
'profileAttributes' => $this->createAttributesDisplayList($profile),
]);
}

/**
* Create attributes display list.
*
* @param UserProfile $profile
*
* @return array
*/
private function createAttributesDisplayList(UserProfile $profile): array
{
$profileAttributes = [];
foreach ($profile->getAttributesList() as $attribute) {
switch ($attribute->getName()) {
case UserProfile::ATTR_SELFIE:
case UserProfile::ATTR_FULL_NAME:
// Selfie and full name are handled separately.
break;
case UserProfile::ATTR_GIVEN_NAMES:
$profileAttributes[] = $this->createAttributeDisplayItem($attribute, 'Given names', 'yoti-icon-profile');
break;
case UserProfile::ATTR_FAMILY_NAME:
$profileAttributes[] = $this->createAttributeDisplayItem($attribute, 'Family names', 'yoti-icon-profile');
break;
case UserProfile::ATTR_DATE_OF_BIRTH:
$profileAttributes[] = $this->createAttributeDisplayItem($attribute, 'Date of Birth', 'yoti-icon-calendar');
break;
case UserProfile::ATTR_GENDER:
$profileAttributes[] = $this->createAttributeDisplayItem($attribute, 'Gender', 'yoti-icon-gender');
break;
case UserProfile::ATTR_STRUCTURED_POSTAL_ADDRESS:
$profileAttributes[] = $this->createAttributeDisplayItem($attribute, 'Structured Postal Address', 'yoti-icon-address');
break;
case UserProfile::ATTR_POSTAL_ADDRESS:
$profileAttributes[] = $this->createAttributeDisplayItem($attribute, 'Address', 'yoti-icon-address');
break;
case UserProfile::ATTR_PHONE_NUMBER:
$profileAttributes[] = $this->createAttributeDisplayItem($attribute, 'Mobile number', 'yoti-icon-phone');
break;
case UserProfile::ATTR_NATIONALITY:
$profileAttributes[] = $this->createAttributeDisplayItem($attribute, 'Nationality', 'yoti-icon-nationality');
break;
case UserProfile::ATTR_EMAIL_ADDRESS:
$profileAttributes[] = $this->createAttributeDisplayItem($attribute, 'Email address', 'yoti-icon-email');
break;
case UserProfile::ATTR_DOCUMENT_DETAILS:
$profileAttributes[] = $this->createAttributeDisplayItem($attribute, 'Document Details', 'yoti-icon-profile');
break;
case UserProfile::ATTR_DOCUMENT_IMAGES:
$profileAttributes[] = $this->createAttributeDisplayItem($attribute, 'Document Images', 'yoti-icon-profile');
break;
default:
// Skip age verifications (name containing ":").
if (strpos($attribute->getName(), ':') === false) {
$profileAttributes[] = $this->createAttributeDisplayItem(
$attribute,
ucwords(str_replace('_', ' ', $attribute->getName())),
'yoti-icon-profile'
);
}
}
}

// Add age verifications.
$ageVerifications = $profile->getAgeVerifications();
if ($ageVerifications) {
foreach ($ageVerifications as $ageVerification) {
$profileAttributes[] = [
'name' => 'Age Verification',
'obj' => $ageVerification->getAttribute(),
'age_verification' => $ageVerification,
'icon' => 'yoti-icon-profile',
];
}
}

return $profileAttributes;
}

/**
* Create attribute display item.
*
* @param Attribute $attribute
* @param string $displayName
* @param string $iconClass
*
* @return array
*/
private function createAttributeDisplayItem(Attribute $attribute, string $displayName, string $iconClass): array
{
return [
'name' => $displayName,
'obj' => $attribute,
'icon' => $iconClass,
];
}
}
Loading
Loading