Skip to content

Commit 8c010f5

Browse files
Object to BaseObject, and SamlSettings
1 parent aedbcf0 commit 8c010f5

File tree

6 files changed

+85
-8
lines changed

6 files changed

+85
-8
lines changed

README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,26 @@ Yii configuration straightforward, just add the following in your config/web.php
4848
```
4949
'user' => [
5050
'class' => 'lucidprogrammer\simplesamlphp\SamlUser',
51+
//idAttribute is mandatory
52+
//for example, if your IDP is sending a SAML payload which has ID, you may do as follows
53+
'idAttribute' => 'ID',
54+
//if you want to map IDP provided attributes to something else, you may do additional mappings as name value pairs.
55+
//following are some examples, not mandatory
56+
'firstName' => 'givenName',
57+
'company' => 'companyName',
5158
],
5259
5360
```
61+
ADFS example,
62+
```
63+
'user' => [
64+
'class' => 'lucidprogrammer\simplesamlphp\SamlUser',
65+
//for example, if your IDP is ADFS, and you want to use email address as the unique ID
66+
'idAttribute' => 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress',
67+
],
68+
69+
```
70+
5471

5572
### Note on enabling authentication for a route using yii2
5673

@@ -80,6 +97,14 @@ After the component is installed, the moment you hit the site/about page, it sho
8097
So, if you want to do SAML provided attributes and want to implement a fine grained access control, yii2 makes it easy.
8198

8299
### Note on yii2 login link.
83-
If your application has links to login, for example, 'site/#', you need to change to _saml/#.
100+
If your application has links to login, for example, 'site/#', you need to change to _saml/#._
84101

85102
However, it is best if you use Yii::$app->user->loginUrl[0], so it will take whatever is the correct loginUrl, so it will work with or without this plugin.
103+
104+
# Changelog
105+
106+
02 March 2018
107+
http://www.yiiframework.com/doc-2.0/yii-base-object.html
108+
The class name `Object` is invalid since PHP 7.2, use [[BaseObject]] instead.
109+
Added SamlSettings options for easy configuration.
110+
Tested with ADFS 3.0, Windows 2012 R2 & simplesamlphp (IDP)

src/Saml.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
*/
1111

1212
namespace lucidprogrammer\simplesamlphp;
13-
use yii\base\Object;
13+
use yii\base\BaseObject;
1414

15-
class Saml extends Object {
15+
class Saml extends BaseObject {
1616

1717
/**
1818
* Authentication source you will use.

src/SamlIdentity.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515

1616
use yii;
17-
use yii\base\Object;
17+
use yii\base\BaseObject;
1818
use yii\web\IdentityInterface;
1919
use lucidprogrammer\simplesamlphp\SamlIdentity;
2020

21-
class SamlIdentity extends Object implements IdentityInterface {
21+
class SamlIdentity extends BaseObject implements IdentityInterface {
2222

2323
public $id;
2424
public $attributes;
@@ -43,8 +43,9 @@ public static function findIdentity($id)
4343
{
4444
$attributes = Yii::$container->get('saml')->getAttributes();
4545
if(sizeof($attributes) > 0){
46+
// just in case the user didn't set idAttribute, give something anyway, he can troubleshoot later instead of throwing errors here
4647
$id = mt_rand();
47-
$uniqueIdentifierFromIdp = getenv('IDP_PROVIDED_USER_IDENTIFIER_NAME') ? getenv('IDP_PROVIDED_USER_IDENTIFIER_NAME') : '';
48+
$uniqueIdentifierFromIdp = Yii::$container->get('samlsettings')->idAttribute ? Yii::$container->get('samlsettings')->idAttribute : '';
4849
if($uniqueIdentifierFromIdp){
4950
$id = $attributes[$uniqueIdentifierFromIdp] && count($attributes[$uniqueIdentifierFromIdp])>0 ? $attributes[$uniqueIdentifierFromIdp][0] : $id;
5051
}
@@ -86,7 +87,15 @@ public static function findIdentityByAccessToken($token, $type = null)
8687

8788
public function __get($name)
8889
{
89-
return isset($this->attributes[$name]) ? $this->attributes[$name][0] : null;
90+
$result = null;
91+
$mappings = Yii::$container->get('samlsettings')->mappings;
92+
if(isset($mappings[$name]))
93+
{
94+
$result = isset($this->attributes[$mappings[$name]]) ? $this->attributes[$mappings[$name]][0] : null;
95+
} else {
96+
$result = isset($this->attributes[$name]) ? $this->attributes[$name][0] : null;
97+
}
98+
return $result;
9099
}
91100

92101

src/SamlSettings.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Saml Object which uses the simplesamlphp project
4+
*
5+
* @see https://simplesamlphp.org
6+
* @author Lucid Programmer<lucidprogrammer@hotmail.com>
7+
* @copyright 2017 Lucid Programmer
8+
* @license https://github.com/lucidprogrammer/yii2-simplesamlphp/blob/master/README.md
9+
* @link https://github.com/lucidprogrammer/yii2-simplesamlphp
10+
*/
11+
12+
namespace lucidprogrammer\simplesamlphp;
13+
use yii\base\BaseObject;
14+
15+
class SamlSettings extends BaseObject {
16+
var $idAttribute;
17+
var $mappings;
18+
19+
public function __construct ( $idAttribute, $mappings=[], $config = [] ){
20+
$this->idAttribute = $idAttribute;
21+
$this->mappings = $mappings;
22+
parent::__construct ( $config = [] );
23+
}
24+
25+
26+
27+
}

src/SamlUser.php

+15
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,24 @@
1313

1414
use yii;
1515
use yii\web\User;
16+
use ArrayObject;
17+
use lucidprogrammer\simplesamlphp\SamlSettings;
1618

1719
class SamlUser extends User
1820
{
21+
22+
function __construct($attributes=[]) {
23+
$idAttribute = null;
24+
$mappings = null;
25+
if(array_key_exists('idAttribute', $attributes)){
26+
$idAttribute = $attributes['idAttribute'];
27+
$mappings = (new ArrayObject($attributes))->getArrayCopy();
28+
unset($mappings['idAttribute']);
29+
$mappings = array_values($mappings);
30+
}
31+
Yii::$container->set('samlsettings',new SamlSettings($idAttribute,$mappings,[]));
32+
parent::__construct();
33+
}
1934
/**
2035
* changing the loginUrl and identityClass
2136
* so while configuring yii2, just point to the user class and all other auth rules should automatically work.

src/_SamlController.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ public function actionLogin(){
2222
Yii::$container->get('saml')->requireAuth();
2323
} else {
2424
$attributes = Yii::$container->get('saml')->getAttributes();
25+
// just in case the user didn't set idAttribute, give something anyway, he can troubleshoot later instead of throwing errors here
2526
$id = mt_rand();
26-
$uniqueIdentifierFromIdp = getenv('IDP_PROVIDED_USER_IDENTIFIER_NAME') ? getenv('IDP_PROVIDED_USER_IDENTIFIER_NAME') : '';
27+
$uniqueIdentifierFromIdp = Yii::$container->get('samlsettings')->idAttribute ? Yii::$container->get('samlsettings')->idAttribute : '';
2728
if($uniqueIdentifierFromIdp){
2829
$id = $attributes[$uniqueIdentifierFromIdp] && count($attributes[$uniqueIdentifierFromIdp])>0 ? $attributes[$uniqueIdentifierFromIdp][0] : $id;
2930
}

0 commit comments

Comments
 (0)