-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSso.php
50 lines (43 loc) · 1.1 KB
/
Sso.php
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
<?php
namespace sammaye\discourse;
use Yii;
use yii\base\Component;
class Sso extends Component
{
public $secret;
public function validate($payload, $sig)
{
$payload = urldecode($payload);
if(hash_hmac("sha256", $payload, $this->secret) === $sig) {
return true;
} else {
return false;
}
}
public function getNonce($payload)
{
$payload = urldecode($payload);
$query = array();
parse_str(base64_decode($payload), $query);
if(isset($query["nonce"])) {
return $query["nonce"];
} else {
throw new Exception("Nonce not found in payload!");
}
}
public function buildLoginString($params)
{
if(!isset($params["external_id"])) {
throw new Exception("Missing required parameter 'external_id'");
}
if(!isset($params["nonce"])) {
throw new Exception("Missing required parameter 'nonce'");
}
if(!isset($params["email"])) {
throw new Exception("Missing required parameter 'email'");
}
$payload = base64_encode(http_build_query($params));
$sig = hash_hmac("sha256", $payload, $this->secret);
return http_build_query(array("sso" => $payload, "sig" => $sig));
}
}