summaryrefslogtreecommitdiff
path: root/examples/openid20/gsasl-openid20-rp.php
blob: 3038c8ec39725dd892f0dc765c5d27da3e9b830f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php # -*- mode: php -*-

// gsasl-openid20-rp.php --- OpenID RP for smtp-server-openid20.c.
// Copyright (C) 2012-2025 Simon Josefsson
//
// This file is part of GNU SASL.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

require_once "Auth/OpenID/Consumer.php";
require_once "Auth/OpenID/FileStore.php";
require_once "Auth/OpenID/SReg.php";
session_start ();

$store_path = '/tmp/gsasl-openid20-store';
$store = new Auth_OpenID_FileStore($store_path);
$consumer = new Auth_OpenID_Consumer($store);

$matches = array();
if (preg_match (",^/([A-Za-z0-9]+)$,", $_SERVER['PATH_INFO'], $matches) == 1) {
  $nonce = $matches[1];
}

$return_to = file_get_contents ("$store_path/state/$nonce/return_to");

$response = $consumer->complete($return_to);

if ($response->status == Auth_OpenID_CANCEL) {

  file_put_contents ("$store_path/state/$nonce/fail",
		     "openid.error=cancel");

  print "OpenID authentication cancelled";

} else if ($response->status == Auth_OpenID_FAILURE) {

  file_put_contents ("$store_path/state/$nonce/fail",
		     "openid.error=failure");

  print "OpenID authentication failed: " . $response->message;

} else if ($response->status == Auth_OpenID_SUCCESS) {

  $claimed = $response->identity_url;

  $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
  $sreg = $sreg_resp->contents();
  $outcome = "";
  if (@$sreg['email']) {
    $outcome .= "&email=" . urlencode($sreg['email']);
  }
  if (@$sreg['nickname']) {
    $outcome .= "&nickname=" . urlencode($sreg['nickname']);
  }
  if (@$sreg['fullname']) {
    $outcome .= "&fullname=" . urlencode($sreg['fullname']);
  }
  $outcome = trim ($outcome, "&");

  file_put_contents ("$store_path/state/$nonce/sreg", $outcome);
  file_put_contents ("$store_path/state/$nonce/success", $claimed);

  print "Congratulations!  You are authenticated as: " . $claimed;
}

?>