Add Gitlab authentication

This commit is contained in:
Frederic Guillot
2015-08-23 13:15:59 -04:00
parent 297c11e48e
commit 8eb739bb91
40 changed files with 501 additions and 9 deletions

122
app/Auth/Gitlab.php Normal file
View File

@@ -0,0 +1,122 @@
<?php
namespace Auth;
use Event\AuthEvent;
/**
* Gitlab backend
*
* @package auth
*/
class Gitlab extends Base
{
/**
* Backend name
*
* @var string
*/
const AUTH_NAME = 'Gitlab';
/**
* OAuth2 instance
*
* @access private
* @var \Core\OAuth2
*/
private $service;
/**
* Authenticate a Gitlab user
*
* @access public
* @param string $gitlab_id Gitlab user id
* @return boolean
*/
public function authenticate($gitlab_id)
{
$user = $this->user->getByGitlabId($gitlab_id);
if (! empty($user)) {
$this->userSession->refresh($user);
$this->container['dispatcher']->dispatch('auth.success', new AuthEvent(self::AUTH_NAME, $user['id']));
return true;
}
return false;
}
/**
* Unlink a Gitlab account for a given user
*
* @access public
* @param integer $user_id User id
* @return boolean
*/
public function unlink($user_id)
{
return $this->user->update(array(
'id' => $user_id,
'gitlab_id' => '',
));
}
/**
* Update the user table based on the Gitlab profile information
*
* @access public
* @param integer $user_id User id
* @param array $profile Gitlab profile
* @return boolean
*/
public function updateUser($user_id, array $profile)
{
$user = $this->user->getById($user_id);
return $this->user->update(array(
'id' => $user_id,
'gitlab_id' => $profile['id'],
'email' => $profile['email'] ?: $user['email'],
'name' => $profile['name'] ?: $user['name'],
));
}
/**
* Get OAuth2 configured service
*
* @access public
* @return \Core\OAuth2
*/
public function getService()
{
if (empty($this->service)) {
$this->service = $this->oauth->createService(
GITLAB_CLIENT_ID,
GITLAB_CLIENT_SECRET,
$this->helper->url->to('oauth', 'gitlab', array(), '', true),
GITLAB_OAUTH_AUTHORIZE_URL,
GITLAB_OAUTH_TOKEN_URL,
array()
);
}
return $this->service;
}
/**
* Get Gitlab profile
*
* @access public
* @param string $code
* @return array
*/
public function getProfile($code)
{
$this->getService()->getAccessToken($code);
return $this->httpClient->getJson(
GITLAB_API_URL.'user',
array($this->getService()->getAuthorizationHeader())
);
}
}

View File

@@ -30,6 +30,16 @@ class Oauth extends Base
$this->step1('github');
}
/**
* Link or authenticate a Gitlab account
*
* @access public
*/
public function gitlab()
{
$this->step1('gitlab');
}
/**
* Unlink external account
*

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1054,4 +1054,12 @@ return array(
'Link type' => 'Type de lien',
'Change task color when using a specific task link' => 'Changer la couleur de la tâche lorsqu\'un lien spécifique est utilisé',
'Task link creation or modification' => 'Création ou modification d\'un lien sur une tâche',
'Login with my Gitlab Account' => 'Se connecter avec mon compte Gitlab',
'Milestone' => 'Étape importante',
'Gitlab Authentication' => 'Authentification Gitlab',
'Help on Gitlab authentication' => 'Aide sur l\'authentification Gitlab',
'Gitlab Id' => 'Identifiant Gitlab',
'Gitlab Account' => 'Compte Gitlab',
'Link my Gitlab Account' => 'Lier mon compte Gitlab',
'Unlink my Gitlab Account' => 'Ne plus utiliser mon compte Gitlab',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -1052,4 +1052,12 @@ return array(
// 'Link type' => '',
// 'Change task color when using a specific task link' => '',
// 'Task link creation or modification' => '',
// 'Login with my Gitlab Account' => '',
// 'Milestone' => '',
// 'Gitlab Authentication' => '',
// 'Help on Gitlab authentication' => '',
// 'Gitlab Id' => '',
// 'Gitlab Account' => '',
// 'Link my Gitlab Account' => '',
// 'Unlink my Gitlab Account' => '',
);

View File

@@ -23,7 +23,7 @@ class Acl extends Base
'webhook' => '*',
'ical' => '*',
'feed' => '*',
'oauth' => array('google', 'github'),
'oauth' => array('google', 'github', 'gitlab'),
);
/**

View File

@@ -138,6 +138,22 @@ class User extends Base
return $this->db->table(self::TABLE)->eq('github_id', $github_id)->findOne();
}
/**
* Get a specific user by the Gitlab id
*
* @access public
* @param string $gitlab_id Gitlab user id
* @return array|boolean
*/
public function getByGitlabId($gitlab_id)
{
if (empty($gitlab_id)) {
return false;
}
return $this->db->table(self::TABLE)->eq('gitlab_id', $gitlab_id)->findOne();
}
/**
* Get a specific user by the username
*

View File

@@ -6,7 +6,12 @@ use PDO;
use Core\Security;
use Model\Link;
const VERSION = 84;
const VERSION = 85;
function version_85($pdo)
{
$pdo->exec("ALTER TABLE users ADD COLUMN gitlab_id INT");
}
function version_84($pdo)
{

View File

@@ -6,7 +6,12 @@ use PDO;
use Core\Security;
use Model\Link;
const VERSION = 64;
const VERSION = 65;
function version_65($pdo)
{
$pdo->exec("ALTER TABLE users ADD COLUMN gitlab_id INTEGER");
}
function version_64($pdo)
{

View File

@@ -6,7 +6,12 @@ use Core\Security;
use PDO;
use Model\Link;
const VERSION = 80;
const VERSION = 81;
function version_81($pdo)
{
$pdo->exec("ALTER TABLE users ADD COLUMN gitlab_id INTEGER");
}
function version_80($pdo)
{

View File

@@ -15,7 +15,7 @@
<?= $this->form->label(t('Password'), 'password') ?>
<?= $this->form->password('password', $values, $errors, array('required')) ?>
<?php if ($captcha): ?>
<?php if (isset($captcha) && $captcha): ?>
<?= $this->form->label(t('Enter the text below'), 'captcha') ?>
<img src="<?= $this->url->href('auth', 'captcha') ?>"/>
<?= $this->form->text('captcha', $values, $errors, array('required')) ?>
@@ -31,14 +31,18 @@
</form>
<?php endif ?>
<?php if (GOOGLE_AUTH || GITHUB_AUTH): ?>
<?php if (GOOGLE_AUTH || GITHUB_AUTH || GITLAB_AUTH): ?>
<ul class="no-bullet">
<?php if (GOOGLE_AUTH): ?>
<li><?= $this->url->link(t('Login with my Google Account'), 'oauth', 'google') ?></li>
<?php endif ?>
<?php if (GITHUB_AUTH): ?>
<li><?= $this->url->link(t('Login with my Github Account'), 'oauth', 'gitHub') ?></li>
<li><?= $this->url->link(t('Login with my Github Account'), 'oauth', 'github') ?></li>
<?php endif ?>
<?php if (GITLAB_AUTH): ?>
<li><?= $this->url->link(t('Login with my Gitlab Account'), 'oauth', 'gitlab') ?></li>
<?php endif ?>
</ul>
<?php endif ?>

View File

@@ -18,6 +18,12 @@
<p class="form-help"><a href="http://kanboard.net/documentation/github-authentication" target="_blank"><?= t('Help on Github authentication') ?></a></p>
</div>
<h3><img src="<?= $this->url->dir() ?>assets/img/gitlab-icon.png"/>&nbsp;<?= t('Gitlab Authentication') ?></h3>
<div class="listing">
<input type="text" class="auto-select" readonly="readonly" value="<?= $this->url->href('oauth', 'gitlab', array(), false, '', true) ?>"/><br/>
<p class="form-help"><a href="http://kanboard.net/documentation/gitlab-authentication" target="_blank"><?= t('Help on Gitlab authentication') ?></a></p>
</div>
<h3><img src="<?= $this->url->dir() ?>assets/img/mailgun-icon.png"/>&nbsp;<?= t('Mailgun (incoming emails)') ?></h3>
<div class="listing">
<input type="text" class="auto-select" readonly="readonly" value="<?= $this->url->href('webhook', 'mailgun', array('token' => $values['webhook_token']), false, '', true) ?>"/><br/>

View File

@@ -14,6 +14,9 @@
<?= $this->form->label(t('Github Id'), 'github_id') ?>
<?= $this->form->text('github_id', $values, $errors) ?>
<?= $this->form->label(t('Gitlab Id'), 'gitlab_id') ?>
<?= $this->form->text('gitlab_id', $values, $errors) ?>
<?= $this->form->checkbox('is_ldap_user', t('Remote user'), 1, isset($values['is_ldap_user']) && $values['is_ldap_user'] == 1) ?>
<?= $this->form->checkbox('disable_login_form', t('Disallow login form'), 1, isset($values['disable_login_form']) && $values['disable_login_form'] == 1) ?>

View File

@@ -25,6 +25,9 @@
<?= $this->form->label(t('Github Id'), 'github_id') ?>
<?= $this->form->password('github_id', $values, $errors) ?><br/>
<?= $this->form->label(t('Gitlab Id'), 'gitlab_id') ?>
<?= $this->form->password('gitlab_id', $values, $errors) ?><br/>
</div>
<div class="form-column">

View File

@@ -34,6 +34,22 @@
</p>
<?php endif ?>
<?php if (! GOOGLE_AUTH && ! GITHUB_AUTH): ?>
<?php if (GITHUB_AUTH): ?>
<h3><img src="<?= $this->url->dir() ?>assets/img/gitlab-icon.png"/>&nbsp;<?= t('Gitlab Account') ?></h3>
<p class="listing">
<?php if ($this->user->isCurrentUser($user['id'])): ?>
<?php if (empty($user['gitlab_id'])): ?>
<?= $this->url->link(t('Link my Gitlab Account'), 'oauth', 'gitlab', array(), true) ?>
<?php else: ?>
<?= $this->url->link(t('Unlink my Gitlab Account'), 'oauth', 'unlink', array('backend' => 'gitlab'), true) ?>
<?php endif ?>
<?php else: ?>
<?= empty($user['gitlab_id']) ? t('No account linked.') : t('Account linked.') ?>
<?php endif ?>
</p>
<?php endif ?>
<?php if (! GOOGLE_AUTH && ! GITHUB_AUTH && ! GITLAB_AUTH): ?>
<p class="alert"><?= t('No external authentication enabled.') ?></p>
<?php endif ?>

View File

@@ -146,6 +146,7 @@ if (ENABLE_URL_REWRITE) {
// Auth routes
$container['router']->addRoute('oauth/google', 'oauth', 'google');
$container['router']->addRoute('oauth/github', 'oauth', 'github');
$container['router']->addRoute('oauth/gitlab', 'oauth', 'gitlab');
$container['router']->addRoute('login', 'auth', 'login');
$container['router']->addRoute('logout', 'auth', 'logout');
}

View File

@@ -50,6 +50,14 @@ defined('GITHUB_OAUTH_AUTHORIZE_URL') or define('GITHUB_OAUTH_AUTHORIZE_URL', 'h
defined('GITHUB_OAUTH_TOKEN_URL') or define('GITHUB_OAUTH_TOKEN_URL', 'https://github.com/login/oauth/access_token');
defined('GITHUB_API_URL') or define('GITHUB_API_URL', 'https://api.github.com/');
// Gitlab authentication
defined('GITLAB_AUTH') or define('GITLAB_AUTH', false);
defined('GITLAB_CLIENT_ID') or define('GITLAB_CLIENT_ID', '');
defined('GITLAB_CLIENT_SECRET') or define('GITLAB_CLIENT_SECRET', '');
defined('GITLAB_OAUTH_AUTHORIZE_URL') or define('GITLAB_OAUTH_AUTHORIZE_URL', 'https://gitlab.com/oauth/authorize');
defined('GITLAB_OAUTH_TOKEN_URL') or define('GITLAB_OAUTH_TOKEN_URL', 'https://gitlab.com/oauth/token');
defined('GITLAB_API_URL') or define('GITLAB_API_URL', 'https://gitlab.com/api/v3/');
// Proxy authentication
defined('REVERSE_PROXY_AUTH') or define('REVERSE_PROXY_AUTH', false);
defined('REVERSE_PROXY_USER_HEADER') or define('REVERSE_PROXY_USER_HEADER', 'REMOTE_USER');