-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwp-authentication-kit.php
127 lines (105 loc) · 3.08 KB
/
wp-authentication-kit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/*
Plugin Name: WordPress Authentication Kit
Description: Login to WordPress and WP-Rest-API via social networks.
Version: 2.0.0
Author: Yanik Peiffer
Text Domain: wp_authentication_kit
Domain Path: /languages/
*/
defined( 'ABSPATH' ) or die( 'No!' );
define( 'WAK_PLUGIN_DIR', plugin_dir_path(__FILE__) );
define( 'WAK_PLUGIN_DIR_URL', plugin_dir_url(__FILE__) );
define( 'WAK_JWT_ENDPOINT_NAMESPACE', 'wp-jwt/v1' );
/**
* Require composer autoload to load necessary classes.
*/
if (!file_exists(WAK_PLUGIN_DIR . '/vendor/autoload.php')) {
throw new Error('Please execute `composer install` in ' . WAK_PLUGIN_DIR . ' and try again! | ');
} else {
require_once WAK_PLUGIN_DIR . '/vendor/autoload.php';
}
class WP_Authentication_Kit {
public function __construct() {
$this->add_hooks();
$this->load_classes();
}
/**
* Load all classes.
*
* @since 0.0.1
*
* @return void
*/
function load_classes() {
/**
* Require jwt-functions to use them in this plugin.
*/
require_once WAK_PLUGIN_DIR.'inc/class-wak-functions.php';
/**
* Require jwt-admin if user is in backend
*/
if( is_admin() ) {
require_once WAK_PLUGIN_DIR.'inc/class-wak-admin.php';
}
/**
* Require jwt-login-endpoint to register endpoint.
*/
require_once WAK_PLUGIN_DIR.'inc/class-wak-login-endpoint.php';
}
/**
* Add wp-hooks.
*
* @since 0.0.1
*
* @return void
*/
function add_hooks() {
add_filter( 'determine_current_user', array($this, 'rest_jwt_auth_handler'), 20 );
add_action( 'init', array($this, 'load_textdomain') );
if( empty(get_option('jwt_secret')) ) {
add_action( 'admin_notices', array($this, 'required_jwt_secret') );
}
}
/**
* Load textdomain
*/
function load_textdomain() {
$domain = 'wp_jwt_auth';
$locale = apply_filters('plugin_locale', get_locale(), $domain);
load_textdomain($domain, WP_LANG_DIR.'/wp-jwt-authentication/'.$domain.'-'.$locale.'.mo');
load_plugin_textdomain($domain, FALSE, dirname(plugin_basename(__FILE__)).'/languages/');
}
/**
* Show error if no secret is set
*
* @since 1.1.0
*/
function required_jwt_secret() {
$class = 'notice notice-error';
$message = __( 'In order to use JWT for the Rest-API you have to set a <strong>secret</strong>. <a href="/wp-admin/options-general.php?page=jwt_admin_page&tab=general">Go to settings</a>', 'wp_jwt_auth' );
printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message );
}
/**
* Add jwt-validation to wp-authorization.
*
* Uses 'validate_token' in order to validate the token from the current request.
*
* @since 0.0.1
*
* @param string $user The user from current authorization.
* @return int Logged in user id.
*/
function rest_jwt_auth_handler($user) {
$jwt_functions = new WAK_Functions();
$jwt_return = $jwt_functions->validate_token();
if( ! $jwt_return ) {
return $user;
}
return $jwt_return;
}
}
if( class_exists( 'WP_Authentication_Kit' ) ) {
$wp_authentication_kit = new WP_Authentication_Kit();
}
?>