Archived
Initial commit
This commit is contained in:
commit
2430d56362
8 files changed
+1788
No files matched your search
@@ -0,0 +1,2 @@
|
||||
/vendor
|
||||
/wordpress
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
class GaltinnAuth {
|
||||
var $require_is_volunteer = true;
|
||||
|
||||
public function __construct($run_wp_hooks = true) {
|
||||
if ($run_wp_hooks) {
|
||||
// Hook before wp_authenticate_username_password and wp_authenticate_email_password at pri 20
|
||||
add_filter('authenticate', array($this, 'authenticate'), 10, 3);
|
||||
// FIXME: run require_is_volunteer_filter
|
||||
}
|
||||
}
|
||||
|
||||
function authenticate($user, $username, $password) {
|
||||
/* Override authenticate function
|
||||
* Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/authenticate
|
||||
**/
|
||||
if ($user instanceof WP_User) {
|
||||
return $user;
|
||||
}
|
||||
if (empty($username) && empty($password) && is_wp_error($user)) {
|
||||
return $user;
|
||||
}
|
||||
|
||||
$galtinn_data = $this->get_galtinn_user($username, $password);
|
||||
|
||||
if ($galtinn_data->status_code !== 200) {
|
||||
$error_msg = json_decode($galtinn_data->body, true)['detail'];
|
||||
return new WP_Error('invalid_username_or_password',
|
||||
__("<strong>ERROR</strong>: $error_msg") .
|
||||
' <a href="' . wp_lostpassword_url() . '">' .
|
||||
__('Lost your password?') .
|
||||
'</a>'
|
||||
);
|
||||
}
|
||||
|
||||
$galtinn_user = json_decode($galtinn_data->body, true);
|
||||
if ($this->require_is_volunteer && !$galtinn_user['is_volunteer']) {
|
||||
$error_msg = 'User with email ' . $galtinn_user['email'] . ' is not a volunteer, which is required to log in.';
|
||||
return new WP_Error('invalid_volunteer_status', __("<strong>ERROR</strong>: $error_msg"));
|
||||
}
|
||||
return $this->get_or_create_user(
|
||||
$galtinn_user['username'],
|
||||
$password,
|
||||
$galtinn_user['email'],
|
||||
$galtinn_user['first_name'],
|
||||
$galtinn_user['last_name']);
|
||||
}
|
||||
|
||||
function get_galtinn_user($username, $password) {
|
||||
/* Authenticate by trying to get the user profile */
|
||||
$headers = ['Accept' => 'application/json'];
|
||||
$options = ['auth' => [$username, $password]];
|
||||
$request = Requests::get(GALTINN_URL, $headers, $options);
|
||||
return $request;
|
||||
}
|
||||
|
||||
function get_or_create_user($username, $password, $email, $first_name, $last_name) {
|
||||
$userdata = [
|
||||
'user_login' => $username,
|
||||
'user_pass' => $password,
|
||||
'user_email' => $email,
|
||||
'first_name' => $first_name,
|
||||
'last_name' => $last_name];
|
||||
$existing_user = get_user_by('login', $username);
|
||||
|
||||
if (!$existing_user) {
|
||||
$new_user_id = wp_insert_user($userdata);
|
||||
if (is_wp_error($new_user_id)) {
|
||||
error_log("[error] " . $new_user_id->get_error_message() . "(username: $username)");
|
||||
}
|
||||
return get_user_by('id', $new_user_id);
|
||||
}
|
||||
/* Update data */
|
||||
$userdata['ID'] = $existing_user->ID;
|
||||
|
||||
$updated_user_id = wp_insert_user($userdata);
|
||||
if (is_wp_error($updated_user_id)) {
|
||||
error_log("[error] " . $updated_user_id->get_error_message() . "(username: $username)");
|
||||
}
|
||||
return get_user_by('id', $updated_user_id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
Wordpress plugin for authenticating with Galtinn.
|
||||
|
||||
|
||||
## Development
|
||||
composer
|
||||
phpunit # FIXME
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^6",
|
||||
"johnpbloch/wordpress": "^4.9",
|
||||
"rmccue/requests": "^1.7"
|
||||
}
|
||||
}
|
||||
Generated
+1652
File diff suppressed because it is too large.
Load diff
+18
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/*
|
||||
Plugin Name: Galtinn Authentication
|
||||
Plugin URI: https://galtinn.neuf.no
|
||||
Description: Authenticate with username or email with galtinn.neuf.no
|
||||
Version: 0.1.0
|
||||
Author: EDB-gjengen
|
||||
Author URI: https://edb.technology
|
||||
License: MIT
|
||||
*/
|
||||
|
||||
if (!defined('GALTINN_URL')) {
|
||||
define('GALTINN_URL', 'https://galtinn.neuf.no/api/me/basic/');
|
||||
}
|
||||
|
||||
require_once 'GaltinnAuth.php';
|
||||
|
||||
new GaltinnAuth();
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
require_once '../GaltinnAuth.php';
|
||||
|
||||
define('GALTINN_URL', 'http://127.0.0.1:8000/api/me/basic/');
|
||||
|
||||
final class AuthTest extends TestCase {
|
||||
public function testCanBeCreatedFromValidEmailAddress(){
|
||||
$username = 'admin';
|
||||
$pasword = 'admin';
|
||||
$galtinn = new GaltinnAuth($run_wp_hooks=false);
|
||||
|
||||
$res = $galtinn->get_galtinn_user($username, $pasword);
|
||||
|
||||
$this->assertEquals(200, $res->status_code, var_export($res, true));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user