Bladeren bron

jwt helper

tags/0.1.0
Robin Thoni 8 jaren geleden
bovenliggende
commit
1093b8cab4
1 gewijzigde bestanden met toevoegingen van 50 en 0 verwijderingen
  1. 50
    0
      src/Auth/Business/JwtHelper.php

+ 50
- 0
src/Auth/Business/JwtHelper.php Bestand weergeven

@@ -0,0 +1,50 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 9/27/15
6
+ * Time: 1:51 AM
7
+ */
8
+
9
+namespace Luticate\Auth\Business;
10
+
11
+class JwtHelper
12
+{
13
+    const EXPIRATION_KEY =  "expiration_date";
14
+    const USER_KEY =        "user_id";
15
+
16
+    public static function decode($token)
17
+    {
18
+        $jwt = mcrypt_decrypt(MCRYPT_TRIPLEDES, env("MCRYPT_KEY"), base64_decode($token), "ecb");
19
+        try
20
+        {
21
+            $data = (array)\JWT::decode($jwt, env("JWT_KEY"), ['HS256']);
22
+        }
23
+        catch (\Exception $e)
24
+        {
25
+            return null;
26
+        }
27
+
28
+        $expiration_date = array_key_exists(self::EXPIRATION_KEY, $data) ? $data[self::EXPIRATION_KEY] : null;
29
+        if (!is_numeric($expiration_date))
30
+            return null;
31
+        $user_id = array_key_exists(self::USER_KEY, $data) ? $data[self::USER_KEY] : null;
32
+        if (!is_numeric($user_id))
33
+            return null;
34
+
35
+        if ($expiration_date < time())
36
+            return null;
37
+
38
+        return $data;
39
+    }
40
+
41
+    public static function encode($data)
42
+    {
43
+        $date = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
44
+        $date->modify("+30 day");
45
+        $date->setTime(0, 0, 0);
46
+        $data[self::EXPIRATION_KEY] = $date->getTimestamp();
47
+
48
+        return base64_encode(mcrypt_encrypt(MCRYPT_TRIPLEDES, env("MCRYPT_KEY"), \JWT::encode($data, env("JWT_KEY")), "ecb"));
49
+    }
50
+}

Laden…
Annuleren
Opslaan