|
| 1 | +--- |
| 2 | +title: Sample code for Age Verification |
| 3 | +excerpt: The following samples show how to use the [Open Gateway Age Verification API](https://opengateway.telefonica.com/en/apis/age-verification) to check if a subscriber meets a required age threshold, optionally including content lock or parental control information. |
| 4 | +category: 681879c3afc1a0003709c745 |
| 5 | +--- |
| 6 | + |
| 7 | +The following code shows, for didactic purposes, a hypothetical or sample SDK, in several programming languages, from a generic Open Gateway's channel partner, also known as aggregator. |
| 8 | + |
| 9 | +The final implementation will depend on the channel partner's development tools offering. Sample code on how to consume the API without an SDK, directly with HTTP requests, is also provided. |
| 10 | + |
| 11 | +> 📘 Want to try it before coding? |
| 12 | +> Check the [API interactive reference](https://developers.opengateway.telefonica.com/reference/verifyage) |
| 13 | +
|
| 14 | +### Table of contents |
| 15 | +- [Backend flow](#backend-flow) |
| 16 | + - [Authorization](#authorization) |
| 17 | + - [API usage](#api-usage) |
| 18 | +- [Frontend flow](#frontend-flow) |
| 19 | + - [Authorization code request](#authorization-code-request) |
| 20 | + - [Callback handling](#callback-handling) |
| 21 | + |
| 22 | +## Backend flow |
| 23 | + |
| 24 | +The authorization protocol used in Open Gateway for backend flows is the OIDC standard CIBA (Client-Initiated Backchannel Authentication). You can check the CAMARA documentation on this flow [here](https://github.com/camaraproject/IdentityAndConsentManagement/blob/release-0.1.0/documentation/CAMARA-API-access-and-user-consent.md#ciba-flow-backend-flow). |
| 25 | + |
| 26 | +### Authorization |
| 27 | + |
| 28 | +```python Sample SDK for Python |
| 29 | +from aggregator_opengateway_sdk import ClientCredentials, AgeVerifier |
| 30 | + |
| 31 | +credentials = ClientCredentials( |
| 32 | + client_id='my-app-id', |
| 33 | + client_secret='my-app-secret' |
| 34 | +) |
| 35 | + |
| 36 | +age_client = AgeVerifier(credentials=credentials, phone_number='+34629255833') |
| 37 | +``` |
| 38 | + |
| 39 | +```node Sample SDK for Node.js |
| 40 | +import { ClientCredentials, AgeVerifier } from "aggregator/opengateway-sdk" |
| 41 | + |
| 42 | +const credentials = new ClientCredentials({ |
| 43 | + clientId: 'my-app-id', |
| 44 | + clientSecret: 'my-app-secret' |
| 45 | +}) |
| 46 | + |
| 47 | +const customerPhoneNumber = '+34629255833' |
| 48 | + |
| 49 | +const ageClient = new AgeVerifier(credentials, undefined, customerPhoneNumber) |
| 50 | +``` |
| 51 | + |
| 52 | +```java Sample SDK for Java |
| 53 | +import aggregator.opengatewaysdk.ClientCredentials; |
| 54 | +import aggregator.opengatewaysdk.AgeVerifier; |
| 55 | + |
| 56 | +ClientCredentials credentials = new ClientCredentials( |
| 57 | + "my-app-id", |
| 58 | + "my-app-secret" |
| 59 | +); |
| 60 | + |
| 61 | +final String customerPhoneNumber = "+34629255833"; |
| 62 | + |
| 63 | +AgeVerifier ageClient = new AgeVerifier(credentials, null, customerPhoneNumber); |
| 64 | +``` |
| 65 | + |
| 66 | +```javascript HTTP using JavaScript (ES6) |
| 67 | +let clientId = "my-app-id"; |
| 68 | +let clientSecret = "my-app-secret"; |
| 69 | +let appCredentials = btoa(`${clientId}:${clientSecret}`); |
| 70 | +let scope = "dpv:FraudPreventionAndDetection kyc-age-verification:verify"; |
| 71 | + |
| 72 | +const tokenRequest = await fetch("https://opengateway.aggregator.com/token", { |
| 73 | + method: "POST", |
| 74 | + headers: { |
| 75 | + "Content-Type": "application/x-www-form-urlencoded", |
| 76 | + "Authorization": `Basic ${appCredentials}` |
| 77 | + }, |
| 78 | + body: new URLSearchParams({ scope }) |
| 79 | +}); |
| 80 | + |
| 81 | +const { access_token } = await tokenRequest.json(); |
| 82 | +``` |
| 83 | + |
| 84 | +### API usage |
| 85 | + |
| 86 | +```python Sample SDK for Python |
| 87 | +result = age_client.verify_age({ |
| 88 | + "ageThreshold": 18, |
| 89 | + "idDocument": "12345678A", |
| 90 | + "givenName": "Federica", |
| 91 | + "familyName": "Sanchez", |
| 92 | + "birthdate": "1990-05-12", |
| 93 | + "email": "user@example.com", |
| 94 | + "includeContentLock": True, |
| 95 | + "includeParentalControl": True |
| 96 | +}) |
| 97 | + |
| 98 | +print("Age check passed?", result.ageCheck) |
| 99 | +print("Verified ID?", result.verifiedStatus) |
| 100 | +print("Match score:", result.identityMatchScore) |
| 101 | +print("Content lock:", result.contentLock) |
| 102 | +print("Parental control:", result.parentalControl) |
| 103 | +``` |
| 104 | + |
| 105 | +```node Sample SDK for Node.js |
| 106 | +const result = await ageClient.verifyAge({ |
| 107 | + ageThreshold: 18, |
| 108 | + idDocument: "12345678A", |
| 109 | + givenName: "Federica", |
| 110 | + familyName: "Sanchez", |
| 111 | + birthdate: "1990-05-12", |
| 112 | + email: "user@example.com", |
| 113 | + includeContentLock: true, |
| 114 | + includeParentalControl: true |
| 115 | +}) |
| 116 | + |
| 117 | +console.log("Age check passed?", result.ageCheck) |
| 118 | +console.log("Verified ID?", result.verifiedStatus) |
| 119 | +console.log("Match score:", result.identityMatchScore) |
| 120 | +console.log("Content lock:", result.contentLock) |
| 121 | +console.log("Parental control:", result.parentalControl) |
| 122 | +``` |
| 123 | + |
| 124 | +```java Sample SDK for Java |
| 125 | +Map<String, Object> verificationData = new HashMap<>(); |
| 126 | +verificationData.put("ageThreshold", 18); |
| 127 | +verificationData.put("idDocument", "12345678A"); |
| 128 | +verificationData.put("givenName", "Federica"); |
| 129 | +verificationData.put("familyName", "Sanchez"); |
| 130 | +verificationData.put("birthdate", "1990-05-12"); |
| 131 | +verificationData.put("email", "user@example.com"); |
| 132 | +verificationData.put("includeContentLock", true); |
| 133 | +verificationData.put("includeParentalControl", true); |
| 134 | + |
| 135 | +AgeVerificationResult result = ageClient.verifyAge(verificationData); |
| 136 | + |
| 137 | +System.out.println("Age check passed? " + result.getAgeCheck()); |
| 138 | +System.out.println("Verified ID? " + result.getVerifiedStatus()); |
| 139 | +System.out.println("Match score: " + result.getIdentityMatchScore()); |
| 140 | +System.out.println("Content lock: " + result.getContentLock()); |
| 141 | +System.out.println("Parental control: " + result.getParentalControl()); |
| 142 | +``` |
| 143 | + |
| 144 | +```javascript HTTP using JavaScript (ES6) |
| 145 | +const headers = new Headers(); |
| 146 | +headers.append("Authorization", `Bearer ${access_token}`); |
| 147 | +headers.append("Content-Type", "application/json"); |
| 148 | + |
| 149 | +const payload = JSON.stringify({ |
| 150 | + ageThreshold: 18, |
| 151 | + idDocument: "12345678A", |
| 152 | + givenName: "Federica", |
| 153 | + familyName: "Sanchez", |
| 154 | + birthdate: "1990-05-12", |
| 155 | + email: "user@example.com", |
| 156 | + includeContentLock: true, |
| 157 | + includeParentalControl: true |
| 158 | +}); |
| 159 | + |
| 160 | +const response = await fetch("https://opengateway.aggregator.com/kyc-age-verification/v0.1/verify", { |
| 161 | + method: "POST", |
| 162 | + headers, |
| 163 | + body: payload |
| 164 | +}); |
| 165 | + |
| 166 | +const result = await response.json(); |
| 167 | +console.log("Age check:", result.ageCheck); |
| 168 | +console.log("Verified status:", result.verifiedStatus); |
| 169 | +console.log("Match score:", result.identityMatchScore); |
| 170 | +``` |
| 171 | + |
| 172 | +--- |
| 173 | + |
| 174 | +## Frontend flow |
| 175 | + |
| 176 | +### Authorization code request |
| 177 | + |
| 178 | +```javascript HTTP using JavaScript (ES6) |
| 179 | +let clientId = "my-app-id"; |
| 180 | +let redirectUri = "https://myapp.com/age-callback"; |
| 181 | +let scope = "dpv:FraudPreventionAndDetection kyc-age-verification:verify"; |
| 182 | + |
| 183 | +const params = new URLSearchParams({ |
| 184 | + client_id: clientId, |
| 185 | + response_type: "code", |
| 186 | + scope, |
| 187 | + redirect_uri: redirectUri |
| 188 | +}); |
| 189 | + |
| 190 | +window.location.href = `https://opengateway.aggregator.com/authorize?${params.toString()}`; |
| 191 | +``` |
| 192 | + |
| 193 | +### Callback handling |
| 194 | + |
| 195 | +```python HTTP using Python + Flask |
| 196 | +from flask import Flask, request |
| 197 | +import requests, base64 |
| 198 | + |
| 199 | +client_id = "my-app-id" |
| 200 | +client_secret = "my-app-secret" |
| 201 | + |
| 202 | +app = Flask(__name__) |
| 203 | + |
| 204 | +@app.route('/age-callback', methods=['GET']) |
| 205 | +def callback(): |
| 206 | + code = request.args.get('code') |
| 207 | + credentials = base64.b64encode(f"{client_id}:{client_secret}".encode()).decode() |
| 208 | + headers = { |
| 209 | + "Content-Type": "application/x-www-form-urlencoded", |
| 210 | + "Authorization": f"Basic {credentials}" |
| 211 | + } |
| 212 | + data = { |
| 213 | + "grant_type": "authorization_code", |
| 214 | + "code": code, |
| 215 | + "redirect_uri": "https://myapp.com/age-callback" |
| 216 | + } |
| 217 | + response = requests.post("https://opengateway.aggregator.com/token", headers=headers, data=data) |
| 218 | + access_token = response.json().get("access_token") |
| 219 | + return access_token |
| 220 | + |
| 221 | +if __name__ == '__main__': |
| 222 | + app.run() |
0 commit comments