Changeset 1168653
- Timestamp:
- 05/27/2015 03:30:37 PM (11 years ago)
- Location:
- wp-jwt-auth/trunk
- Files:
-
- 6 edited
-
JWT_AUTH.php (modified) (2 diffs)
-
README.md (modified) (1 diff)
-
lib/JWT_AUTH_Admin.php (modified) (2 diffs)
-
lib/JWT_AUTH_Options.php (modified) (1 diff)
-
lib/JWT_AUTH_UserProcessor.php (modified) (7 diffs)
-
lib/JWT_AUTH_UsersRepo.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
wp-jwt-auth/trunk/JWT_AUTH.php
r1160505 r1168653 3 3 * Plugin Name: Wordpress JWT Authentication 4 4 * Description: Implements JWT Authentication for APIs 5 * Version: 1. 0.15 * Version: 1.1.0 6 6 * Author: Auth0 7 7 * Author URI: https://auth0.com … … 22 22 add_filter("plugin_action_links_$plugin", array(__CLASS__, 'wp_add_plugin_settings_link')); 23 23 24 add_action( 'init', array( __CLASS__, 'add_headers' ), 99 ); 25 26 JWT_AUTH_UsersRepo::init(); 24 27 JWT_AUTH_UserProcessor::init(); 25 28 JWT_AUTH_Settings_Section::init(); 26 29 JWT_AUTH_Admin::init(); 30 } 31 32 public static function add_headers() { 33 header('Access-Control-Allow-Origin:'. get_http_origin()); 34 header('Access-Control-Allow-Credentials: true'); 35 36 if ( 'OPTIONS' == $_SERVER['REQUEST_METHOD'] ) { 37 if ( isset( $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] ) ) { 38 header('Access-Control-Allow-Methods: GET, POST, OPTIONS'); 39 } 40 if ( isset( $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] ) ) { 41 header('Access-Control-Allow-Headers: ' . $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']); 42 } 43 die('options'); 44 } 45 } 46 public static function send_cors_headers( $headers ) { 47 $headers['Access-Control-Allow-Origin'] = get_http_origin(); // Can't use wildcard origin for credentials requests, instead set it to the requesting origin 48 $headers['Access-Control-Allow-Credentials'] = 'true'; 49 // Access-Control headers are received during OPTIONS requests 50 if ( 'OPTIONS' == $_SERVER['REQUEST_METHOD'] ) { 51 if ( isset( $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] ) ) { 52 $headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'; 53 } 54 if ( isset( $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] ) ) { 55 $headers['Access-Control-Allow-Headers'] = $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']; 56 } 57 } 58 return $headers; 27 59 } 28 60 -
wp-jwt-auth/trunk/README.md
r1156142 r1168653 9 9 - **User Property**: is the property which much match with the JWT attribute to determine the user. 10 10 - **JWT Attribute**: should match the User Property to determine the user. 11 - **User Repository**: by default it should be empty. If you want to override the way the user matching is made (ie: you need to look to another table in the database) you can create your own User Repostory and match the user as you need. The user repository should expose one static method called `getUser` that receives the decoded JWT and should receive a `WP_User` object. 11 12 ##Overriding the User Repository logic 13 The user repository is the responsible of retriving the user based on the JWT. By default, it looks in the user database to match the *User Property* and the *JWT Attribute*. 14 15 If you need to override the way the user matching is made (ie: you need to look to another table in the database) you can create your own User Repostory and match the user as you need. 16 17 To accomplish this, you need to add a filter: 18 19 ``` 20 add_filter( 'wp_jwt_auth_get_user', array( __CLASS__, 'getUser' ),10); 21 ``` 22 23 To see an example, check the [UsersRepo](https://github.com/auth0/wp-jwt-auth/blob/master/lib/JWT_AUTH_UsersRepo.php). 12 24 13 25 > When the plugin is using a User Repository the *User Property* and *JWT Property* settings are ignored. -
wp-jwt-auth/trunk/lib/JWT_AUTH_Admin.php
r1160505 r1168653 54 54 array('id' => 'jwt_auth_user_property', 'name' => 'User Property', 'function' => 'render_user_property'), 55 55 array('id' => 'jwt_auth_jwt_attribute', 'name' => 'JWT Attribute', 'function' => 'render_jwt_attribute'), 56 array('id' => 'jwt_auth_override_user_repo', 'name' => 'User Repository', 'function' => 'render_override_user_repo'),57 56 58 57 )); … … 90 89 echo '<br/><span class="description">' . __('JWT Attribute the plugin should use to match the users.', JWT_AUTH_LANG) . '</span>'; 91 90 } 92 public static function render_override_user_repo(){93 $v = JWT_AUTH_Options::get( 'override_user_repo' );94 echo '<input type="text" name="' . JWT_AUTH_Options::OPTIONS_NAME . '[override_user_repo]" id="jwt_auth_override_user_repo" value="' . esc_attr( $v ) . '"/>';95 echo '<br/><span class="description">' . __('The User Repository is how this plugin looks for the users related to the token. When it is empty, it will work searching for a user which matchs the User Property and the JWT Attribute. If it is not empty, a custom repository is configured (probably from another plugin or custom configruation) and will ignore the User Property setting.', JWT_AUTH_LANG) . '</span>';96 }97 91 98 92 public static function render_settings_page(){ -
wp-jwt-auth/trunk/lib/JWT_AUTH_Options.php
r1156142 r1168653 39 39 'user_property' => 'id', 40 40 'jwt_attribute' => 'sub', 41 'override_user_repo' => false,42 41 'secret_base64_encoded' => false, 43 42 ); -
wp-jwt-auth/trunk/lib/JWT_AUTH_UserProcessor.php
r1156142 r1168653 5 5 public static function init() { 6 6 7 add_filter( 'woocommerce_api_check_authentication', array(__CLASS__, 'determine_current_user_for_wc'), 10); 7 8 add_filter( 'determine_current_user', array(__CLASS__, 'determine_current_user'), 10); 8 9 add_filter( 'json_authentication_errors', array(__CLASS__, 'json_authentication_errors')); … … 40 41 } 41 42 42 protected static function findUser($jwt ) {43 protected static function findUser($jwt, $encodedJWT) { 43 44 $overrideUserRepo = JWT_AUTH_Options::get('override_user_repo'); 44 45 45 if ($overrideUserRepo) { 46 return call_user_func(array($overrideUserRepo, 'getUser'), $jwt); 47 } 48 else { 49 return JWT_AUTH_UsersRepo::getUser($jwt); 50 } 46 return apply_filters( 'wp_jwt_auth_get_user', $jwt, $encodedJWT ); 51 47 } 52 48 53 public static function determine_current_user ($user) 49 public static function determine_current_user_for_wc($user) { 50 return self::determine_current_user_generic($user, true); 51 } 52 53 public static function determine_current_user ($user) { 54 return self::determine_current_user_generic($user, false); 55 } 56 public static function determine_current_user_generic ($user, $returnUserObj) 54 57 { 55 58 global $wp_json_basic_auth_error; … … 59 62 $authorization = self::getAuthorizationHeader(); 60 63 61 if ($authorization !== false) { 64 $authorization = str_replace('Bearer ', '', $authorization); 65 66 if ($authorization !== '') { 62 67 63 68 try { … … 69 74 } 70 75 71 $objuser = self::findUser($token );76 $objuser = self::findUser($token, $authorization); 72 77 73 78 if (!$objuser) { 74 79 $wp_json_basic_auth_error = 'Invalid user'; 80 return null; 75 81 } 76 82 77 $user = $objuser->ID; 83 if ($returnUserObj) { 84 $user = $objuser; 85 } 86 else { 87 $user = $objuser->ID; 88 } 78 89 } 79 90 … … 83 94 } 84 95 85 protected static function decodeJWT($ authorization)96 protected static function decodeJWT($encUser) 86 97 { 87 98 require_once JWT_AUTH_PLUGIN_DIR . 'lib/php-jwt/Exceptions/BeforeValidException.php'; … … 98 109 } 99 110 100 $encUser = str_replace('Bearer ', '', $authorization);101 102 111 try { 103 112 // Decode the user … … 109 118 } 110 119 } catch(\UnexpectedValueException $e) { 111 die($e->getMessage());112 120 throw new Exception($e->getMessage()); 113 121 } -
wp-jwt-auth/trunk/lib/JWT_AUTH_UsersRepo.php
r1156142 r1168653 3 3 class JWT_AUTH_UsersRepo { 4 4 5 public static function getUser($jwt) { 5 public static function init() { 6 add_filter( 'wp_jwt_auth_get_user', array( __CLASS__, 'getUser' ),10); 7 } 8 9 public static function getUser($jwt) { 6 10 global $wpdb; 7 11 12 if ($jwt instanceof WP_User) return $jwt; 13 14 $user_property = esc_sql(JWT_AUTH_Options::get('user_property')); 8 15 $jwt_attribute = JWT_AUTH_Options::get('jwt_attribute'); 9 16 17 if (trim($user_property) == '' || trim($jwt_attribute) == '') return; 18 10 19 $id = $jwt->$jwt_attribute; 11 12 $user_property = esc_sql(JWT_AUTH_Options::get('user_property'));13 20 14 21 $sql = 'SELECT u.*
Note: See TracChangeset
for help on using the changeset viewer.