同期実装
はじめに
このガイドでは、ユーザーをリダイレクトした後にバックエンドが直接応答するのを待つ、同期的なアプローチを使ってサイレント認証を実装する方法を説明します。
注:シンクロナス実装を使用している場合、アプリケーション・ダッシュボードでVerify機能をオンにすると、次のようになります。 任意, これは、ステータスウェブフック(Synchronous実装には不要)を受信する機能を切り替えるだけだからです。
以下の図に、同期バージョンの Silent Authentication を実装するために必要な手順を示す:
リクエスト検証コード
サイレント認証プロセスを開始するには、以下のリクエストを行う。 /verify エンドポイントを使用します。次の例では ワークフロー は、Verify が最初にサイレント認証を使用しようとすることを指定します。何らかの理由で要求が失敗した場合、SMS にフォールバックし、音声通話 OTP に続く。
注:複数のチャンネルを使用する場合、サイレント認証 マスト ワークフローの最初のチャンネルである。
サンプルを実行するには、サンプルコード内の以下の変数を独自の値に置き換えてください:
| 可変 | 説明 |
|---|---|
JWT | を使用して API リクエストを認証します。 JWT. |
VERIFY_BRAND_NAME | 認証メッセージでユーザーに表示される会社名またはサービス名。 |
VONAGE_APPLICATION_PRIVATE_KEY_PATH | アプリケーションの秘密鍵。 |
VONAGE_APPLICATION_ID | アプリケーションのアプリケーションID。 |
VERIFY_NUMBER | E.164形式のOTP送信先電話番号(例. +44111223344). |
Write the code
Add the following to request.sh:
curl -X POST "https://api.nexmo.com/v2/verify" \
-H "Authorization: Bearer $JWT"\
-H 'Content-Type: application/json' \
-d $'{
"brand": "'$VERIFY_BRAND_NAME'",
"workflow": [
{
"channel": "silent_auth",
"to": "'$VERIFY_NUMBER'"
},
{
"channel": "sms",
"to": "'$VERIFY_NUMBER'"
},
{
"channel": "voice",
"to": "'$VERIFY_NUMBER'"
}
]
}'Run your code
Save this file to your machine and run it:
Prerequisites
npm install @vonage/server-sdk @vonage/verify2Create a file named send-request-with-fallback.js and add the following code:
const { Vonage } = require('@vonage/server-sdk');
const { Channels, SilentAuthChannel } = require('@vonage/verify2');
const vonage = new Vonage({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_APPLICATION_PRIVATE_KEY_PATH,
});Write the code
Add the following to send-request-with-fallback.js:
vonage.verify2.newRequest({
brand: VERIFY_BRAND_NAME,
workflow: [
{
channel: SilentAuthChannel.SILENT_AUTH,
to: VERIFY_NUMBER,
},
{
channel: Channels.SMS,
to: VERIFY_NUMBER,
},
{
channel: Channels.VOICE,
to: VERIFY_NUMBER,
},
],
})
.then(({requestId}) => console.log(requestId))
.catch((err) => console.error(err));Run your code
Save this file to your machine and run it:
Prerequisites
Add the following to build.gradle:
Create a class named SendVerificationRequestWithFallback and add the following code to the main method:
Write the code
Add the following to the main method of the SendVerificationRequestWithFallback class:
Run your code
We can use the アプリケーション plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
Run the following gradle command to execute your application, replacing com.vonage.quickstart.kt.verify with the package containing SendVerificationRequestWithFallback:
Prerequisites
Add the following to build.gradle:
Create a class named SendRequestWithFallback and add the following code to the main method:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();Write the code
Add the following to the main method of the SendRequestWithFallback class:
VerificationResponse response = client.getVerify2Client().sendVerification(
VerificationRequest.builder()
.addWorkflow(new SilentAuthWorkflow(VERIFY_NUMBER))
.addWorkflow(new SmsWorkflow(VERIFY_NUMBER))
.addWorkflow(new VoiceWorkflow(VERIFY_NUMBER))
.brand(VERIFY_BRAND_NAME).build()
);Run your code
We can use the アプリケーション plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
Run the following gradle command to execute your application, replacing com.vonage.quickstart.verify2 with the package containing SendRequestWithFallback:
Prerequisites
Install-Package VonageCreate a file named StartVerificationRequestWithFallback.cs and add the following code:
Add the following to StartVerificationRequestWithFallback.cs:
Prerequisites
composer require vonage/clientCreate a file named request.php and add the following code:
Run your code
Save this file to your machine and run it:
Prerequisites
pip install vonage python-dotenvRun your code
Save this file to your machine and run it:
Prerequisites
gem install vonageCreate a file named request.rb and add the following code:
client = Vonage::Client.new(
application_id: VONAGE_APPLICATION_ID,
private_key: VONAGE_PRIVATE_KEY
)Write the code
Add the following to request.rb:
client.verify2.start_verification(
brand: VERIFY_BRAND_NAME,
workflow: [
{
channel: 'silent_auth',
to: VERIFY_NUMBER
},
{
channel: 'sms',
to: VERIFY_NUMBER
},
{
channel: 'voice',
to: VERIFY_NUMBER
}
]
)Run your code
Save this file to your machine and run it:
もしリクエストが成功し、Numbersが 対応を受け取ります。 200 Ok レスポンスを含む request_id そして check_url 体内の
リクエストが期限切れになるか、キャンセルされるまで、 check_url を使用してサイレント認証チェックを行うことができる。この応答を受け取ったら、以下のように check_url 認証しようとしているモバイル・デバイスから。
重要だ: について check_url リクエストは(Wi-Fiではなく)モバイルデータ接続を介して実行する必要があります。これを確実にするために推奨される方法は、デバイスのセルラーネットワークを介してリクエストを自動的にルーティングするVonage Client SDK(iOSまたはAndroid)を使用することです。詳しくは サイレント認証のためのWi-Fiバイパスガイド をご覧ください。
GET]リクエストを行うと、いくつかのHTTPリクエストを受信する。 302 リダイレクトは、ターゲット・デバイスが使用している地域とキャリアに依存する:
リダイレクトに従うと、次のいずれかになります。 HTTP 200 または HTTP 409 レスポンスは、リクエストが有効かどうかに応じて変化する。ネットワークに問題がある場合は、このようなレスポンスが表示される:
潜在的なエラーコードの完全なリストは、以下のページにあります。 API仕様.
リクエストが有効な場合は、次のメッセージが表示されます。 HTTP 200 あなたの request_id そして code:
注:安全な認証チェックを保証し、潜在的な中間者攻撃(man in middle attack)を軽減するために、オリジナルを保存する。 request_id と比較する。 request_id がレスポンスに返される。IDが一致しない場合は、サイレント認証チェックを中止しなければならない。詳細は サンプルアプリケーション を参照されたい。
提供された検証コードを確認する
エンドユーザーがコードを受け取ったら、
/v2/verify/{request_id} エンドポイント {request_id} を、前の通話で受け取ったIDで入力してください。 サンプルを実行するには、サンプルコード内の以下の変数を独自の値に置き換えてください:
| 可変 | 説明 |
|---|---|
JWT | を使用して API リクエストを認証します。 JWT. |
VERIFY_REQUEST_ID | について request_id 前のステップで受け取った。 |
VONAGE_APPLICATION_PRIVATE_KEY_PATH | アプリケーションの秘密鍵。 |
VONAGE_APPLICATION_ID | アプリケーションのアプリケーションID。 |
VERIFY_CODE | エンドユーザーが受信した検証コード |
Write the code
Add the following to check-verification-code.sh:
curl -X POST "https://api.nexmo.com/v2/verify/$VERIFY_REQUEST_ID" \
-H "Authorization: Bearer $JWT"\
-H 'Content-Type: application/json' \
-d $'{
"code": "'$VERIFY_CODE'"
}'Run your code
Save this file to your machine and run it:
Prerequisites
npm install @vonage/server-sdkCreate a file named check-verification-code.js and add the following code:
const { Vonage } = require('@vonage/server-sdk');
const vonage = new Vonage ({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_APPLICATION_PRIVATE_KEY_PATH,
});Write the code
Add the following to check-verification-code.js:
vonage.verify2.checkCode(VERIFY_REQUEST_ID, VERIFY_CODE)
.then((status) => console.log(`The status is ${status}`),
)
.catch((err) => console.error(err));Run your code
Save this file to your machine and run it:
Prerequisites
Add the following to build.gradle:
Create a class named CheckVerificationCode and add the following code to the main method:
Write the code
Add the following to the main method of the CheckVerificationCode class:
Run your code
We can use the アプリケーション plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
Run the following gradle command to execute your application, replacing com.vonage.quickstart.kt.verify with the package containing CheckVerificationCode:
Prerequisites
Add the following to build.gradle:
Create a class named CheckVerificationCode and add the following code to the main method:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();Write the code
Add the following to the main method of the CheckVerificationCode class:
try {
client.getVerify2Client().checkVerificationCode(VERIFY_REQUEST_UUID, VERIFY_CODE);
System.out.println("SUCCESS - code matches!");
}
catch (VerifyResponseException ex) {
switch (ex.getStatusCode()) {
case 400: // Code does not match
case 404: // Already verified or not found
case 409: // Workflow does not support code
case 410: // Incorrect code provided too many times
case 429: // Rate limit exceeded
default: // Unknown or internal server error (500)
ex.printStackTrace();
}
}Run your code
We can use the アプリケーション plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
Run the following gradle command to execute your application, replacing com.vonage.quickstart.verify2 with the package containing CheckVerificationCode:
Prerequisites
Install-Package VonageCreate a file named VerifyCodeRequest.cs and add the following code:
Add the following to VerifyCodeRequest.cs:
Prerequisites
composer require vonage/clientCreate a file named send_code.php and add the following code:
Run your code
Save this file to your machine and run it:
Prerequisites
pip install vonage python-dotenvRun your code
Save this file to your machine and run it:
Prerequisites
gem install vonageCreate a file named check.rb and add the following code:
Run your code
Save this file to your machine and run it:
注:サイレント認証ワークフローのコードは、以下の場合にのみチェックできます。 かつて.
コードが有効であれば、最終的なレスポンスとして以下のステータスが返されます。 completed:
エラーがある場合は「Invalid Code」と表示されます:
この時点で、サイレント認証の検証は完了です。