|
| 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\Object; |
| 14 | + |
| 15 | +class Saml extends Object { |
| 16 | + |
| 17 | + /** |
| 18 | + * Authentication source you will use. |
| 19 | + */ |
| 20 | + public $authSource='default-sp'; |
| 21 | + |
| 22 | + /** |
| 23 | + * SimpleSAML_Auth_Simple's object. |
| 24 | + */ |
| 25 | + private $auth; |
| 26 | + |
| 27 | + |
| 28 | + public function init() { |
| 29 | + $this->auth = new \SimpleSAML_Auth_Simple($this->authSource); |
| 30 | + parent::init(); |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + /** |
| 35 | + * Make sure user is authenticated. If the user is not authenticated, he will be rediected to Simplesamlphp IdP login page. If he is authenticated, it does nothing. |
| 36 | + * @see https://simplesamlphp.org/docs/stable/simplesamlphp-sp-api#section_3 |
| 37 | + */ |
| 38 | + public function requireAuth(array $params = array()) { |
| 39 | + $this->auth->requireAuth($params); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Log in the current user. He will be redirected to Simplesamlphp IdP login page. After a successfull login, he will be redirected to the referer page. |
| 44 | + * @see https://simplesamlphp.org/docs/stable/simplesamlphp-sp-api#section_4 |
| 45 | + */ |
| 46 | + public function login(array $params = array()) { |
| 47 | + $this->auth->login($params); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Logout the current user. Clear Simplesamlphp Sp and Simplesamlphp IdP session and redirected to the referer page. |
| 52 | + * @see https://simplesamlphp.org/docs/stable/simplesamlphp-sp-api#section_5 |
| 53 | + */ |
| 54 | + public function logout($params = NULL) { |
| 55 | + $this->auth->logout($params); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Get login url. |
| 60 | + * @see https://simplesamlphp.org/docs/stable/simplesamlphp-sp-api#section_8 |
| 61 | + */ |
| 62 | + public function getLoginURL($returnTo = null) { |
| 63 | + $this->auth->getLogoutUrl($returnTo); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get logout url. |
| 68 | + * @see https://simplesamlphp.org/docs/stable/simplesamlphp-sp-api#section_9 |
| 69 | + */ |
| 70 | + public function getLogoutURL($returnTo = null) { |
| 71 | + $this->auth->getLogoutUrl($returnTo); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Check wether the user is authenticated or not. |
| 76 | + * @see https://simplesamlphp.org/docs/stable/simplesamlphp-sp-api#section_9 |
| 77 | + * @return bool true if user is authenticated, false it he is not. |
| 78 | + */ |
| 79 | + public function isAuthenticated() { |
| 80 | + return $this->auth->isAuthenticated(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get attributes which are returned from Simplesamlphp IdP after a successfull login. |
| 85 | + * @see https://simplesamlphp.org/docs/stable/simplesamlphp-sp-api#section_6 |
| 86 | + * @return array attributes |
| 87 | + */ |
| 88 | + public function getAttributes() { |
| 89 | + return $this->auth->getAttributes(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get auth data. |
| 94 | + * @see https://simplesamlphp.org/docs/stable/simplesamlphp-sp-api#section_7 |
| 95 | + * @return mixed |
| 96 | + */ |
| 97 | + public function getAuthData(string $name) { |
| 98 | + return $this->auth->getAuthData($name); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get attribute by it's key. |
| 103 | + * @return string the attribute value |
| 104 | + */ |
| 105 | + public function __get($name) { |
| 106 | + return isset($this->getAttributes()[$name]) ? $this->getAttributes()[$name][0] : null; |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments