Node.js バリデーター

improve this page | report issue

概説

IBM Mobile Foundation は、外部リソースにセキュリティー機能を適用するための Node.js フレームワークを提供しています。
Node.js フレームワークは、npm モジュール (passport-mfp-token-validation) として提供されます。

このチュートリアルでは、スコープ (accessRestricted) を使用して、単純な Node.js リソース GetBalance を保護する方法を示します。

前提条件:

passport-mfp-token-validation モジュール

passport-mfp-token-validation モジュールは、MobileFirst Server によって発行されるアクセス・トークンを検証するための認証メカニズムを提供します。

モジュールをインストールするには、以下を実行します。

npm install passport-mfp-token-validation@8.0.X

使用法

  • サンプルでは、express モジュールと passport-mfp-token-validation モジュールが使用されます。

    var express = require('express');
    var passport = require('passport-mfp-token-validation').Passport;
    var mfpStrategy = require('passport-mfp-token-validation').Strategy;
    
  • Strategy を以下のようにセットアップします。

    passport.use(new mfpStrategy({
      authServerUrl: 'http://localhost:9080/mfp/api',
      confClientID: 'testclient',
      confClientPass: 'testclient',
      analytics: {
          onpremise: {
              url: 'http://localhost:9080/analytics-service/rest/v3',
              username: 'admin',
              password: 'admin'
          }
      }
    }));
    
  • authServerUrl: localhost:9080 を実際の MobileFirst Server IP アドレスとポート番号に置き換えてください。
  • confClientIDconfClientPass: 機密クライアント ID とパスワードを MobileFirst Operations Console で定義したものに置き換えてください。
  • analytics: Analytics 項目はオプションです。Analytics のイベントを Mobile Foundation のログに記録する場合にのみ必要です。
    localhost:9080username、および password を Analytics Server の IP アドレス、ポート番号、ユーザー名、およびパスワードに置き換えてください。

  • passport.authenticate を呼び出して、要求を認証します。

    var app = express();
    app.use(passport.initialize());
    
    app.get('/getBalance', passport.authenticate('mobilefirst-strategy', {
        session: false,
        scope: 'accessRestricted'
    }),
    function(req, res) {
        res.send('17364.9');
    });
    
    var server = app.listen(3000, function() {
        var port = server.address().port
        console.log("Sample app listening at http://localhost:%s", port)
    });
    
  • 採用する Strategy は、mobilefirst-strategy にする必要があります。
  • sessionfalse に設定します。
  • scope 名を指定します。

サンプル・アプリケーション

Node.js サンプルをダウンロードします。

サンプルの使用法

  1. サンプルのルート・フォルダーにナビゲートし、コマンド npm install を実行し、続けて、npm start も実行します。
  2. MobileFirst Operations Console で、必ず機密クライアントと秘密鍵の値を更新してください。
  3. UserLogin または PinCodeAttempts のいずれかのセキュリティー検査をデプロイします。
  4. 一致するアプリケーションを登録します。
  5. accessRestricted スコープをセキュリティー検査にマップします。
  6. クライアント・アプリケーションを更新して、サーブレット URL に WLResourceRequest を発行します。
Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.
Last modified on February 28, 2020