2016-07-12 23 views
0

ログインにmachinepack-jwtを使用し、プロジェクトに登録します。それはトークンを正常に作成し、すべてが満了部分を受け入れやすくなります。トークンが期限切れになっているかどうかわからない。私は有効期限を入れても、その時点で期限切れではありません。以下は、トークンの有効期限が切れや期限切れの場合は、ログインページにリダイレクトしたい場合は、私がキャッチしたいログインのための私のバックエンドのコード、下記のmachinepack-jwtでトークンが期限切れになっているかどうかを確認するには

var JWT = require('machinepack-jwt'), 
    Passwords = require('machinepack-passwords'), 
    GoogleAPIsOAuth2v2 = require('machinepack-googleapisoauth2v2'), 
    Facebook = require('machinepack-facebook'), 
    request = require('request'); 
    module.exports = { 
    authenticate : function(req, res) { 
    console.log("login"); 
      User.findOne({ 
      email: req.body.email 
      }, function foundUser(err, user) { 
      if (err) return res.negotiate(err); 
      if (!user) return res.notFound(); 
    Passwords.checkPassword({ 
     passwordAttempt: req.body.password, 
     encryptedPassword: user.password 
    }).exec({ 
     error: function (err){ 
     console.log(err); 
     return res.negotiate(err); 
     }, 
     incorrect: function(){ 
     return res.notFound(); 
     }, 
     success: function(){ 
     JWT.encode({ 
      secret: '17ca644f4f3be572ec33711a40a5b8b4', 
      payload: { 
      id : user.id, 
      email: user.email 
      }, 
      algorithm: 'HS256', 
      expires: 1 
     }).exec({ 
      error: function (err){ 
      return err; 
      }, 
      success: function (result){ 
      JWT.decode({ 
       secret: '17ca644f4f3be572ec33711a40a5b8b4', 
       token : result, 
       payload: { 
       id : user.id, 
       email: user.email 
       }, 
       algorithm: 'HS256', 
       expires: 1 
      }).exec({ 
       error: function (err) { 
        res.send(err); 
       }, 
       success: function(decodedToken){ 
       console.log(decodedToken); 
       console.log(result); 
       res.send({decodedToken,token : result, expires_in:1}); 
       } 
      }) 
      } 
     }); 
     } 
    }); 
    }); 
    } 
} 

フロントエンドのコードがあり、

angular.module('app') 
    .factory('Auth', function($http, LocalService, AccessLevels ,$auth) { 
    return { 
     authorize: function(access) { 
     if (access === AccessLevels.user) { 
      return this.isAuthenticated(); 
     } else { 
      return true; 
     } 
     }, 
     isAuthenticated: function() { 
     return $auth.isAuthenticated(); 
     }, 
     login: function(credentials) { 
     var login = $http.post('/auth/authenticate', credentials); 
     login.success(function(result) { 
     console.log(result); 
      LocalService.set('satellizer_token', result.token); 
      LocalService.set('user', result.user); 
     }); 
     return login; 
     }, 
     logout: function() { 

     LocalService.unset('satellizer_token'); 
     } 
    } 
    }) 

です。トークンが期限切れになっているかどうかを確認する方法

答えて

1

あなただけのあなたの例では、あなたが悪い値でトークンを作成していることを現在の日付

function isExpired(exp){ 
    if (exp){ 
     return exp <= Date.now()/1000; 
    } else { 
     return true; //True if the token has not the expiration time field 
    } 
} 

注意して復号化されたトークンの「EXP」フィールド(有効期限)を確認する必要があります。 1970-01-01T00:00:00Z UTからの秒数でなければなりません。例えばRFC 1分の有効期限は、私がここに安らかなサービス側の検証を入れています

exp = Date.now()/1000 + 60 
+0

場合、これは来るべきですか?バックエンドで? – shamila

+0

あなたのニーズにもよりますが、通常はJWT検証はバックエンドで行われます – pedrofb

+0

こんにちは@ShamilaSallay、最後に解決策を見つけましたか? – pedrofb

0

する必要があります参照してください。このような

@JsonIgnoreProperties(ignoreUnknown =真) インターフェースJWT {

 Instant getExp(); 

     String getAud(); 

     String getIss(); 

} 

デコード

Jwt jwt = null; 

     try { 

      jwt = objectReader.readValue(payload); 

     } catch (IOException e) { 

      throw new AuthenticationException(e); 

     } 

     // assert not expired 
     if (jwt.getExp().isBefore(Instant.now())) { 
      throw new AuthenticationException("auth token expired"); 
     } 
+0

これはJavaです。問題はnode.jsと角度についてです。基本的にjavascript – pedrofb

+0

この種類の検証は、サービスの一部である必要があります。したがって、私は安らかなサービス側の検証を行っています。 – Gangadhar

関連する問題