Skip to content

Commit f81f83d

Browse files
committed
First commit
0 parents  commit f81f83d

19 files changed

Lines changed: 716 additions & 0 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.zip

.travis.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
language: php
2+
sudo: false
3+
4+
php:
5+
- 7.1
6+
- 7.0
7+
- 5.6
8+
- 5.5
9+
- 5.4
10+
- 5.3
11+
12+
env:
13+
global:
14+
- PLUGIN=OAuth2
15+
- KANBOARD_REPO=https://github.com/kanboard/kanboard.git
16+
matrix:
17+
- DB=sqlite
18+
- DB=mysql
19+
- DB=postgres
20+
21+
matrix:
22+
fast_finish: true
23+
24+
install:
25+
- git clone --depth 1 $KANBOARD_REPO
26+
- ln -s $TRAVIS_BUILD_DIR kanboard/plugins/$PLUGIN
27+
28+
before_script:
29+
- cd kanboard
30+
- phpenv config-add tests/php.ini
31+
- composer install
32+
- ls -la plugins/
33+
34+
script:
35+
- phpunit -c tests/units.$DB.xml plugins/$PLUGIN/Test/

Auth/GenericOAuth2Provider.php

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
<?php
2+
3+
namespace Kanboard\Plugin\OAuth2\Auth;
4+
5+
use Kanboard\Core\Base;
6+
use Kanboard\Core\Security\OAuthAuthenticationProviderInterface;
7+
use Kanboard\Plugin\OAuth2\User\GenericOAuth2UserProvider;
8+
9+
/**
10+
* GenericOAuth2Provider
11+
*
12+
* @package Kanboard\Auth
13+
* @author Frederic Guillot
14+
*/
15+
class GenericOAuth2Provider extends Base implements OAuthAuthenticationProviderInterface
16+
{
17+
/**
18+
* User properties
19+
*
20+
* @access private
21+
* @var GenericOAuth2UserProvider
22+
*/
23+
private $userInfo = null;
24+
25+
/**
26+
* OAuth2 instance
27+
*
28+
* @access protected
29+
* @var \Kanboard\Core\Http\OAuth2
30+
*/
31+
protected $service;
32+
33+
/**
34+
* OAuth2 code
35+
*
36+
* @access protected
37+
* @var string
38+
*/
39+
protected $code = '';
40+
41+
/**
42+
* Get authentication provider name
43+
*
44+
* @access public
45+
* @return string
46+
*/
47+
public function getName()
48+
{
49+
return 'OAuth2';
50+
}
51+
52+
/**
53+
* Authenticate the user
54+
*
55+
* @access public
56+
* @return boolean
57+
*/
58+
public function authenticate()
59+
{
60+
$profile = $this->getProfile();
61+
62+
if (! empty($profile)) {
63+
$this->userInfo = new GenericOAuth2UserProvider($this->container, $profile);
64+
return true;
65+
}
66+
67+
return false;
68+
}
69+
70+
/**
71+
* Set Code
72+
*
73+
* @access public
74+
* @param string $code
75+
* @return $this
76+
*/
77+
public function setCode($code)
78+
{
79+
$this->code = $code;
80+
return $this;
81+
}
82+
83+
/**
84+
* Get user object
85+
*
86+
* @access public
87+
* @return GenericOAuth2UserProvider
88+
*/
89+
public function getUser()
90+
{
91+
return $this->userInfo;
92+
}
93+
94+
/**
95+
* Get configured OAuth2 service
96+
*
97+
* @access public
98+
* @return \Kanboard\Core\Http\OAuth2
99+
*/
100+
public function getService()
101+
{
102+
if (empty($this->service)) {
103+
$this->service = $this->oauth->createService(
104+
$this->getClientId(),
105+
$this->getClientSecret(),
106+
$this->helper->url->to('OAuthController', 'handler', array('plugin' => 'OAuth2'), '', true),
107+
$this->getOAuthAuthorizeUrl(),
108+
$this->getOAuthTokenUrl(),
109+
array()
110+
);
111+
}
112+
113+
return $this->service;
114+
}
115+
116+
/**
117+
* Get user profile
118+
*
119+
* @access public
120+
* @return array
121+
*/
122+
public function getProfile()
123+
{
124+
$token = $this->getService()->getAccessToken($this->code);
125+
126+
if (DEBUG) {
127+
$this->logger->debug(__METHOD__.': Got access token: '.(empty($token) ? 'No' : 'Yes'));
128+
$this->logger->debug(__METHOD__.': Fetch user profile from '.$this->getUserAPiUrl());
129+
}
130+
131+
return $this->httpClient->getJson(
132+
$this->getUserAPiUrl(),
133+
array($this->getService()->getAuthorizationHeader())
134+
);
135+
}
136+
137+
/**
138+
* Unlink user
139+
*
140+
* @access public
141+
* @param integer $userId
142+
* @return bool
143+
*/
144+
public function unlink($userId)
145+
{
146+
return $this->userModel->update(array(
147+
'id' => $userId,
148+
'oauth2_user_id' => '',
149+
));
150+
}
151+
152+
/**
153+
* Get client id
154+
*
155+
* @access public
156+
* @return string
157+
*/
158+
public function getClientId()
159+
{
160+
return $this->configModel->get('oauth2_client_id');
161+
}
162+
163+
/**
164+
* Get client secret
165+
*
166+
* @access public
167+
* @return string
168+
*/
169+
public function getClientSecret()
170+
{
171+
return $this->configModel->get('oauth2_client_secret');
172+
}
173+
174+
/**
175+
* Get authorize url
176+
*
177+
* @access public
178+
* @return string
179+
*/
180+
public function getOAuthAuthorizeUrl()
181+
{
182+
return $this->configModel->get('oauth2_authorize_url');
183+
}
184+
185+
/**
186+
* Get token url
187+
*
188+
* @access public
189+
* @return string
190+
*/
191+
public function getOAuthTokenUrl()
192+
{
193+
return $this->configModel->get('oauth2_token_url');
194+
}
195+
196+
/**
197+
* Get User API url
198+
*
199+
* @access public
200+
* @return string
201+
*/
202+
public function getUserAPiUrl()
203+
{
204+
return $this->configModel->get('oauth2_user_api_url');
205+
}
206+
}

Controller/OAuthController.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Kanboard\Plugin\OAuth2\Controller;
4+
5+
use Kanboard\Controller\OAuthController as BaseOAuthController;
6+
7+
/**
8+
* OAuth Controller
9+
*
10+
* @package Kanboard\Controller
11+
* @author Frederic Guillot
12+
*/
13+
class OAuthController extends BaseOAuthController
14+
{
15+
/**
16+
* Handle authentication
17+
*
18+
* @access public
19+
*/
20+
public function handler()
21+
{
22+
$this->step1('OAuth2');
23+
}
24+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Frédéric Guillot
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
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.

Locale/fr_FR/translations.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
return array();

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
plugin=OAuth2
2+
3+
all:
4+
@ echo "Build archive for plugin ${plugin} version=${version}"
5+
@ git archive HEAD --prefix=${plugin}/ --format=zip -o ${plugin}-${version}.zip

Plugin.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Kanboard\Plugin\OAuth2;
4+
5+
use Kanboard\Core\Plugin\Base;
6+
use Kanboard\Core\Security\Role;
7+
use Kanboard\Core\Translator;
8+
use Kanboard\Plugin\OAuth2\Auth\GenericOAuth2Provider;
9+
10+
class Plugin extends Base
11+
{
12+
public function initialize()
13+
{
14+
$this->authenticationManager->register(new GenericOAuth2Provider($this->container));
15+
$this->applicationAccessMap->add('OAuthController', 'handler', Role::APP_PUBLIC);
16+
17+
$this->route->addRoute('/oauth/callback', 'OAuthController', 'handler', 'OAuth2');
18+
19+
$this->template->hook->attach('template:auth:login-form:after', 'OAuth2:auth/login');
20+
$this->template->hook->attach('template:config:integrations', 'OAuth2:config/integration');
21+
$this->template->hook->attach('template:user:external', 'OAuth2:user/external');
22+
$this->template->hook->attach('template:user:authentication:form', 'OAuth2:user/authentication');
23+
$this->template->hook->attach('template:user:create-remote:form', 'OAuth2:user/create_remote');
24+
}
25+
26+
public function onStartup()
27+
{
28+
Translator::load($this->languageModel->getCurrentLanguage(), __DIR__.'/Locale');
29+
}
30+
31+
public function getPluginName()
32+
{
33+
return 'OAuth2';
34+
}
35+
36+
public function getPluginDescription()
37+
{
38+
return t('Generic OAuth2 authentication plugin');
39+
}
40+
41+
public function getPluginAuthor()
42+
{
43+
return 'Frédéric Guillot';
44+
}
45+
46+
public function getPluginVersion()
47+
{
48+
return '1.0.0';
49+
}
50+
51+
public function getPluginHomepage()
52+
{
53+
return 'https://github.com/kanboard/plugin-oauth2';
54+
}
55+
}
56+

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
OAuth2 Authentication
2+
=====================
3+
4+
Generic OAuth2 authentication plugin.
5+
6+
Author
7+
------
8+
9+
- Frédéric Guillot
10+
- License MIT
11+
12+
Requirements
13+
------------
14+
15+
- Kanboard >= 1.0.34
16+
17+
Installation
18+
------------
19+
20+
You have the choice between 3 methods:
21+
22+
1. Install the plugin from the Kanboard plugin manager in one click
23+
2. Download the zip file and decompress everything under the directory `plugins/OAuth2`
24+
3. Clone this repository into the folder `plugins/OAuth2`
25+
26+
Note: Plugin folder is case-sensitive.
27+
28+
Configuration
29+
-------------
30+
31+
Go to the application settings > integrations > OAuth2 Authentication.

0 commit comments

Comments
 (0)