diff --git a/NtpcOpenId.php b/NtpcOpenId.php new file mode 100644 index 0000000..001632e --- /dev/null +++ b/NtpcOpenId.php @@ -0,0 +1,218 @@ +identity = "https://openid.ntpc.edu.tw/"; + $this->setRequried(); + } + + /** + * 解析 Ntpc Data + * + * @return void + */ + private function getNtpcAttributes() + { + $attr = $this->getAttributes(); + $tmp = explode('/', $this->identity); + $ntpcAttr['account'] = end($tmp); // 帳號 + + foreach($this->required as $field_name => $field_value){ + + switch($field_name){ + case 'class_info': + $ntpcAttr['grade'] = substr($attr['pref/language'], 0, 2); // 年級 + $ntpcAttr['class'] = substr($attr['pref/language'], 2, 2); // 班級 + $ntpcAttr['class_no'] = substr($attr['pref/language'], 4, 2); // 座號 + break; + case 'permission_info': + foreach (json_decode($attr['pref/timezone']) as $item) { + $ntpcAttr['workplaces'][$item->id]['school_title'] = + $item->name; // 單位全銜 + $ntpcAttr['workplaces'][$item->id]['role'] = $item->role; // 身分別 + $ntpcAttr['workplaces'][$item->id]['title'] = $item->title; // 職稱別 + $ntpcAttr['workplaces'][$item->id]['groups'] = $item->groups; // 職務別 + $ntpcAttr['workplaces'][$item->id]['groups_string'] = + $item->groups; // 職務別 + } + break; + default: + $ntpcAttr[$field_name] = $attr[$field_value]; + } + } + + return $ntpcAttr; + } + + /** + * 驗證 + * + * @return boolean + */ + public function validate() + { + $ret = parent::validate(); + if ($ret) { + $this->ntpcData = $this->getNtpcAttributes(); + } + return $ret; + } + + /** + * 傳回Ntpc OpenID Data + * + * @return array + */ + public function getNtpcData() + { + return $this->ntpcData; + } + + /** + * 是否具備身份 + * + * @param string $role + * @return boolean + */ + public function hasRole($role) + { + $ret = false; + if (count($this->allowed_school_ids)) { + foreach ( + $this->ntpcData['workplaces'] + as $school_id => $workplace + ) { + foreach ($this->allowed_school_ids as $allow_school_id) { + if ( + $school_id == $allow_school_id and + $workplace['role'] == $role + ) { + $ret = true; + } + } + } + } else { + foreach ( + $this->ntpcData['workplaces'] + as $school_id => $workplace + ) { + if ($workplace['role'] == $role) { + $ret = true; + } + } + } + return $ret; + } + + /** + * 是否具備職稱 + * + * @param string $title + * @return boolean + */ + public function hasTitle($title) + { + $ret = false; + if (count($this->allowed_school_ids)) { + foreach ( + $this->ntpcData['workplaces'] + as $school_id => $workplace + ) { + foreach ($this->allowed_school_ids as $allow_school_id) { + if ( + $school_id == $allow_school_id and + $title == $workplace['title'] + ) { + $ret = true; + } + } + } + } else { + foreach ( + $this->ntpcData['workplaces'] + as $school_id => $workplace + ) { + if ($title == $workplace['title']) { + $ret = true; + } + } + } + return $ret; + } + + /** + * 是否具備職務 + * + * @param string $group + * @return boolean + */ + public function hasGroup($group) + { + $ret = false; + if (count($this->allowed_school_ids)) { + foreach ( + $this->ntpcData['workplaces'] + as $school_id => $workplace + ) { + foreach ($this->allowed_school_ids as $allow_school_id) { + if ( + $school_id == $allow_school_id and + in_array($group, $workplace['groups']) + ) { + $ret = true; + } + } + } + } else { + foreach ( + $this->ntpcData['workplaces'] + as $school_id => $workplace + ) { + if (in_array($group, $workplace['groups'])) { + $ret = true; + } + } + } + return $ret; + } + + public function setAllowedSchool($Ids) + { + if (gettype($Ids) == "array") { + $this->allowed_school_ids = $Ids; + } elseif (gettype($Ids) == "string") { + $this->allowed_school_ids = [$Ids]; + } + } + + public function setRequried($required_type = 0) + { + if ($required_type) { + $this->required = [ + 'nickname' => 'namePerson/friendly', + 'email' => 'contact/email', + 'cname' => 'namePerson', + 'birth' => 'birthDate', + 'gender' => 'person/gender', + 'identity_code' => 'contact/postalCode/home', + 'school_title' => 'contact/country/home', + 'class_info' => 'pref/language', + 'permission_info' => 'pref/timezone', + ]; + } else { + $this->required = [ + 'email' => 'contact/email', + 'cname' => 'namePerson', + 'school_title' => 'contact/country/home', + 'class_info' => 'pref/language', + 'permission_info' => 'pref/timezone', + ]; + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index 3dc0a81..6adaeca 100644 --- a/README.md +++ b/README.md @@ -1,110 +1,98 @@ > **NOTICE** -> I am no longer able to support or maintain this project - if you would like to take over the project, please drop me a line. +> This project is forked from github.com/iignatov/LightOpenID. Extending for Ntpc OpenID service and add some settings. It must run with openid.php together. # LightOpenID Lightweight PHP5 library for easy OpenID authentication. -* `Version....:` [**1.3.1** :arrow_double_down:][1] - ( *see [the change log][2] for details* ) -* `Released on:` March 04, 2016 -* `Source code:` [Official GitHub Repo :octocat:][3] -* `Homepage...:` http://code.google.com/p/lightopenid/ -* `Author.....:` [Mewp][4] +- `Version....:` [**1.3.1** :arrow_double_down:][1] + ( _see [the change log][2] for details_ ) +- `Released on:` March 04, 2016 +- `Source code:` [Official GitHub Repo :octocat:][3] +- `Homepage...:` http://code.google.com/p/lightopenid/ +- `Author.....:` [Mewp][4] + +# NtpcOpenID + +NtpcOpenID library for easy Ntpc OpenID Authentication and Auhtorization. + +- `Version....:` [**1.3.2**][5] +- `Source code:` [Official GitHub Repo :octocat:][6] +- `Author.....:` [Tiebob][7] [1]: https://github.com/iignatov/LightOpenID/archive/master.zip [2]: https://github.com/iignatov/LightOpenID/blob/master/CHANGELOG.md [3]: https://github.com/Mewp/lightopenid [4]: https://github.com/Mewp - +[5]: https://github.com/Tiebob/LightOpenID/archive/master.zip +[6]: https://github.com/Tiebob/LightOpenID +[7]: https://github.com/Tiebob ## Quick start -### Add to composer.json +### Download openid.php and NtpcOpenID.php from github repo above. + +### Sign-on with OpenID in just 2 steps: + +1. Authentication with the provider: + + ```php + $openid = new NtpcOpenID('my-host.example.org'); + $openid->identity = "https://openid.ntpc.edu.tw/"; + header('Location: ' . $openid->authUrl()); + ``` + +2. Verification: + + ```php + $openid = new NtpcOpenID('my-host.example.org'); -```javascript -"repositories": [ - { - "type": "vcs", - "url": "https://github.com/iignatov/LightOpenID" - } -], + if ($openid->mode) { + echo $openid->validate() ? 'Logged in.' : 'Failed!'; + } + ``` -"require": { - "php": ">=5.4.0", - "iignatov/lightopenid": "*" -} +### Design for Ntpc OpenID: + +Without setting `$openid->required` values, it has default values. If full values needed, use `$openid->setRequired(1)`. For example: + +```php +$openid = new NtpcOpenID('my-host.example.org'); +$openid->identity = "https://openid.ntpc.edu.tw/"; +header('Location: ' . $openid->authUrl()); ``` -### Sign-on with OpenID in just 2 steps: - - 1. Authentication with the provider: - - ```php - $openid = new LightOpenID('my-host.example.org'); - - $openid->identity = 'ID supplied by user'; - - header('Location: ' . $openid->authUrl()); - ``` - 2. Verification: - - ```php - $openid = new LightOpenID('my-host.example.org'); - - if ($openid->mode) { - echo $openid->validate() ? 'Logged in.' : 'Failed!'; - } - ``` - -### Support for AX and SREG extensions: - - To use the AX and SREG extensions, specify `$openid->required` and/or `$openid->optional` - before calling `$openid->authUrl()`. These are arrays, with values being AX schema paths - (the 'path' part of the URL). For example: - - ```php - $openid->required = array('namePerson/friendly', 'contact/email'); - $openid->optional = array('namePerson/first'); - ``` - - Note that if the server supports only SREG or OpenID 1.1, these are automaticaly mapped - to SREG names. To get the values use: - - ```php - $openid->getAttributes(); - ``` - - For more information see [USAGE.md](http://github.com/iignatov/LightOpenID/blob/master/USAGE.md). +```php +$openid = new NtpcOpenID('my-host.example.org'); +$openid->setRequired(1); +header('Location: ' . $openid->authUrl()); +``` +After verify correct. To get the values use: -## Requirements +```php +$openid->getNtpcData(); +``` -This library requires PHP >= 5.1.2 with cURL or HTTP/HTTPS stream wrappers enabled. +## Requirements +This library requires PHP >= 5.6 with cURL or HTTP/HTTPS stream wrappers enabled. ## Features -* Easy to use - you can code a functional client in less than ten lines of code. -* Uses cURL if avaiable, PHP-streams otherwise. -* Supports both OpenID 1.1 and 2.0. -* Supports Yadis discovery. -* Supports only stateless/dumb protocol. -* Works with PHP >= 5. -* Generates no errors with `error_reporting(E_ALL | E_STRICT)`. - +- Supports Ntpc OpenID +- Works with PHP >= 5.6 ## Links -* [JavaScript OpenID Selector](http://code.google.com/p/openid-selector/) - +- [JavaScript OpenID Selector](http://code.google.com/p/openid-selector/) - simple user interface that can be used with LightOpenID. -* [HybridAuth](http://hybridauth.sourceforge.net/) - +- [HybridAuth](http://hybridauth.sourceforge.net/) - easy to install and use social sign on PHP library, which uses LightOpenID. -* [OpenID Dev Specifications](http://openid.net/developers/specs/) - +- [OpenID Dev Specifications](http://openid.net/developers/specs/) - documentation for the OpenID extensions and related topics. - ## License [LightOpenID](http://github.com/iignatov/LightOpenID) diff --git a/composer.json b/composer.json index e0a60de..b9c2b93 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,15 @@ { - "name": "iignatov/lightopenid", + "name": "tiebob/lightopenid", "type": "library", - "description": "Lightweight PHP5 library for easy OpenID authentication.", - "keywords": ["openid", "authentication", "security"], - "homepage": "https://github.com/iignatov/LightOpenID", + "description": "Lightweight PHP5 library for NTPC OpenID authentication.", + "keywords": [ + "openid", + "authentication", + "security" + ], + "homepage": "https://github.com/Tiebob/LightOpenID", "license": "MIT", - "version": "1.3.1", + "version": "0.1", "authors": [ { "name": "Mewp", @@ -14,12 +18,19 @@ { "name": "Ignat Ignatov", "homepage": "https://github.com/iignatov/LightOpenID" + }, + { + "name": "Tiebob", + "homepage": "https://github.com/Tiebob/LightOpenID" } ], "require": { - "php": ">=5.2" + "php": ">=5.6" }, "autoload": { - "classmap": ["openid.php", "provider/provider.php"] + "classmap": [ + "openid.php", + "provider/provider.php" + ] } -} +} \ No newline at end of file diff --git a/examples/example-ntpc.php b/examples/example-ntpc.php new file mode 100644 index 0000000..e2e81bb --- /dev/null +++ b/examples/example-ntpc.php @@ -0,0 +1,85 @@ +mode) { + $openid->identity = "https://openid.ntpc.edu.tw/"; + header('Location: ' . $openid->authUrl()); + } + } catch (ErrorException $e) { + echo $e->getMessage(); + } +?> + + + + + + + + + + mode && $openid->validate()) { ?> + getNtpcData(); ?> +

+ 登出 +

+

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
欄位內容
帳號 + +
+
公務信箱
單位簡稱
年級
班級
座號
+ +
+

+

+ +

+ +
+ +
+ + + + \ No newline at end of file diff --git a/openid.php b/openid.php index 14e684b..359f3aa 100644 --- a/openid.php +++ b/openid.php @@ -917,8 +917,9 @@ function validate() # In such case, validation would fail, since we'd send different data than OP # wants to verify. stripslashes() should solve that problem, but we can't # use it when magic_quotes is off. - $value = $this->data['openid_' . str_replace('.','_',$item)]; - $params['openid.' . $item] = function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc() ? stripslashes($value) : $value; + + ## Disable magic_quotes_gpc checking + $params['openid.' . $item] = $this->data['openid_' . str_replace('.','_',$item)]; }