lovenery/lightopenid

1.3.1 2016-07-18 12:18 UTC

This package is not auto-updated.

Last update: 2024-09-20 22:06:48 UTC


README

我从 https://github.com/iignatov/LightOpenID 分支出来
并将其翻译成中文以提高可读性

LightOpenID

轻量级的OpenID认证库(PHP5)

快速开始

添加到 composer.json

composer require lovenery/lightopenid

或者

"repositories": [
  {
    "type": "vcs",
    "url": "https://github.com/lovenery/LightOpenID"
  }
],

"require": {
  "php": ">=5.4.0",
  "lovenery/lightopenid": "1.3.1"
}

两个步骤登录 OpenID

  1. (认证)使用provider进行认证

    $openid = new LightOpenID('my-host.example.org');
    
    $openid->identity = 'ID supplied by user';
    
    header('Location: ' . $openid->authUrl());

    provider然后通过GET发送各种参数,其中一个参数是openid_mode

  2. (验证)验证是否成功

    $openid = new LightOpenID('my-host.example.org');
    
    if ($openid->mode) {
      echo $openid->validate() ? 'Logged in.' : 'Failed!';
    }

注释

将 'my-host.example.org' 改为你的域名。除非你清楚自己在做什么,否则不要使用 $_SERVER['HTTP_HOST']

可选地,你可以设置 $returnUrl$realm (或 $trustRoot,它是别名)。程序的初始值如下

$openid->realm = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];
$openid->returnUrl = $openid->realm . $_SERVER['REQUEST_URI'];

也支持AX和SREG扩展

在调用 $openid->authUrl() 之前,只需设置 $openid->required 和/或 $openid->optional
里面都是数组,内容是AX模式路径(路径是网址的一部分)。例如

$openid->required = array('namePerson/friendly', 'contact/email');
$openid->optional = array('namePerson/first');

获取所需的数据

$openid->getAttributes();

基本设置选项详细(可省略)

常见的AX属性(可省略)

Here is a list of the more common AX attributes (from [axschema.org](http://www.axschema.org/types/)):

Name                    | Meaning
------------------------|---------------
namePerson/friendly     | Alias/Username
contact/email           | Email
namePerson              | Full name
birthDate               | Birth date
person/gender           | Gender
contact/postalCode/home | Postal code
contact/country/home    | Country
pref/language           | Language
pref/timezone           | Time zone

Note that even if you mark some field as required, there is no guarantee that you'll get any
information from a provider. Not all providers support all of these attributes, and some don't
support these extensions at all.

Google, for example, completely ignores optional parameters, and for the required ones, it supports,
according to [it's website](http://code.google.com/apis/accounts/docs/OpenID.html):

* namePerson/first (first name)
* namePerson/last (last name)
* contact/country/home
* contact/email
* pref/language