Skip to content

Commit 49ffcdc

Browse files
committed
Add Token Library in API
1 parent 48e90d9 commit 49ffcdc

17 files changed

+1064
-234
lines changed

license.txt renamed to LICENSE

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
The MIT License (MIT)
1+
MIT License
22

3-
Copyright (c) 2014 - 2018, British Columbia Institute of Technology
3+
Copyright (c) 2018 Jeevan15498
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99
copies of the Software, and to permit persons to whom the Software is
1010
furnished to do so, subject to the following conditions:
1111

12-
The above copyright notice and this permission notice shall be included in
13-
all copies or substantial portions of the Software.
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
1414

1515
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1717
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21-
THE SOFTWARE.
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

application/config/api.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,20 @@
1616
* API Key POST Request Parameter Name
1717
*/
1818
$config['api_key_post_name'] = 'key';
19+
20+
21+
/**
22+
* Set API Timezone
23+
*/
24+
$config['api_timezone'] = 'Asia/Kolkata';
25+
26+
27+
/**
28+
* API Limit database table name
29+
*/
30+
$config['api_limit_table_name'] = 'api_limit';
31+
32+
/**
33+
* API keys database table name
34+
*/
35+
$config['api_keys_table_name'] = 'api_keys';

application/config/jwt.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php defined('BASEPATH') OR exit('No direct script access allowed');
2+
3+
/*
4+
|--------------------
5+
| JWT Secure Key
6+
|--------------------------------------------------------------------------
7+
*/
8+
$config['jwt_key'] = 'eyJ0eXAiOiJKV1QiLCJhbGciTWvLUzI1NiJ9IiRkYXRhIg';
9+
10+
11+
/*
12+
|-----------------------
13+
| JWT Algorithm Type
14+
|--------------------------------------------------------------------------
15+
*/
16+
$config['jwt_algorithm'] = 'HS256';
17+
18+
19+
/*
20+
|-----------------------
21+
| Token Request Header Name
22+
|--------------------------------------------------------------------------
23+
*/
24+
$config['token_header'] = 'authorization';
25+
26+
27+
/*
28+
|-----------------------
29+
| Token Expire Time
30+
31+
| https://www.tools4noobs.com/online_tools/hh_mm_ss_to_seconds/
32+
|--------------------------------------------------------------------------
33+
| ( 1 Day ) : 60 * 60 * 24 = 86400
34+
| ( 1 Hour ) : 60 * 60 = 3600
35+
| ( 1 Minute ) : 60 = 60
36+
*/
37+
$config['token_expire_time'] = 86400;

application/config/routes.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@
5454
$route['translate_uri_dashes'] = FALSE;
5555

5656

57-
$route['api/user/get'] = 'api_test/get_users';
57+
$route['api/user/demo'] = 'api_test/demo';
58+
$route['api/user/login'] = 'api_test/login';
59+
$route['api/user/view'] = 'api_test/view';

application/controllers/Api_test.php

Lines changed: 103 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,123 @@ public function __construct() {
88
parent::__construct();
99
}
1010

11-
public function get_users()
11+
/**
12+
* demo method
13+
*
14+
* @link [api/user/demo]
15+
* @method POST
16+
* @return Response|void
17+
*/
18+
public function demo()
1219
{
1320
header("Access-Control-Allow-Origin: *");
1421

22+
// API Configuration
1523
$this->_apiConfig([
1624
/**
17-
* By Default Method `GET`
25+
* By Default Request Method `GET`
1826
*/
19-
'methods' => ['GET', 'POST', 'OPTIONS'],
27+
'methods' => ['POST'], // 'GET', 'OPTIONS'
28+
2029
/**
21-
* number limit, type limit, time limit (last minute)
30+
* Number limit, type limit, time limit (last minute)
2231
*/
23-
// 'limit' => [5, 'ip', 'everyday'],
32+
'limit' => [5, 'ip', 'everyday'],
2433

2534
/**
2635
* type :: ['header', 'get', 'post']
2736
* key :: ['table : Check Key in Database', 'key']
2837
*/
29-
// 'key' => ['GET'], // type, {key}|table (by default)
38+
'key' => ['POST', $this->key() ], // type, {key}|table (by default)
3039
]);
3140

32-
33-
// echo "HELLO WORLD";
41+
// return data
42+
$this->api_return(
43+
[
44+
'status' => true,
45+
"result" => "Return API Response",
46+
],
47+
200);
48+
}
49+
50+
/**
51+
* Check API Key
52+
*
53+
* @return key|string
54+
*/
55+
private function key()
56+
{
57+
// use database query for get valid key
58+
59+
return 1452;
60+
}
61+
62+
63+
/**
64+
* login method
65+
*
66+
* @link [api/user/login]
67+
* @method POST
68+
* @return Response|void
69+
*/
70+
public function login()
71+
{
72+
header("Access-Control-Allow-Origin: *");
73+
74+
// API Configuration
75+
$this->_apiConfig([
76+
'methods' => ['POST'],
77+
]);
78+
79+
// you user authentication code will go here, you can compare the user with the database or whatever
80+
$payload = [
81+
'id' => "Your User's ID",
82+
'other' => "Some other data"
83+
];
84+
85+
// Load Authorization Library or Load in autoload config file
86+
$this->load->library('authorization_token');
87+
88+
// generte a token
89+
$token = $this->authorization_token->generateToken($payload);
90+
91+
// return data
92+
$this->api_return(
93+
[
94+
'status' => true,
95+
"result" => [
96+
'token' => $token,
97+
],
98+
99+
],
100+
200);
101+
}
102+
103+
/**
104+
* view method
105+
*
106+
* @link [api/user/view]
107+
* @method POST
108+
* @return Response|void
109+
*/
110+
public function view()
111+
{
112+
header("Access-Control-Allow-Origin: *");
113+
114+
// API Configuration [Return Array: User Token Data]
115+
$user_data = $this->_apiConfig([
116+
'methods' => ['POST'],
117+
'requireAuthorization' => true,
118+
]);
119+
120+
// return data
121+
$this->api_return(
122+
[
123+
'status' => true,
124+
"result" => [
125+
'user_data' => $user_data['token_data']
126+
],
127+
],
128+
200);
34129
}
35130
}

0 commit comments

Comments
 (0)