aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Identity/OAuth2Storage.php
blob: a4ba9c526955ed1b74c2f96055084b2cacae1708 (plain) (blame)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php

namespace Zotlabs\Identity;


class OAuth2Storage extends \OAuth2\Storage\Pdo {

    /**
     * @param string $username
     * @param string $password
     * @return bool
     */
    public function checkUserCredentials($username, $password)
    {
        if ($user = $this->getUser($username)) {
            return $this->checkPassword($user, $password);
        }

        return false;
    }

    /**
     * @param string $username
     * @return array|bool
     */
    public function getUserDetails($username)
    {
        return $this->getUser($username);
    }


    /**
     *
     * @param array $user
     * @param string $password
     * @return bool
     */
    protected function checkPassword($user, $password)
    {

		$x = account_verify_password($user,$password);
		return((array_key_exists('channel',$x) && ! empty($x['channel'])) ? true : false);

    }

    /**
     * @param string $username
     * @return array|bool
     */
    public function getUser($username)
    {

		$x = channelx_by_n($username);
		if(! $x) {
			return false;
		}

		$a = q("select * from account where account_id = %d",
			intval($x['channel_account_id'])
		);

		$n = explode(' ', $x['channel_name']);

		return( [
			'webfinger'   => channel_reddress($x),
			'portable_id' => $x['channel_hash'],
			'email'       => $a[0]['account_email'],
			'username'    => $x['channel_address'],
			'user_id'     => $x['channel_id'],
			'name'        => $x['channel_name'],
			'firstName'   => ((count($n) > 1) ? $n[1] : $n[0]),
			'lastName'    => ((count($n) > 2) ? $n[count($n) - 1] : ''),
			'picture'     => $x['xchan_photo_l']
		] );
    }

    public function scopeExists($scope) {
      // Report that the scope is valid even if it's not.
      // We will only return a very small subset no matter what.
      // @TODO: Truly validate the scope
      //    see vendor/bshaffer/oauth2-server-php/src/OAuth2/Storage/ScopeInterface.php and
      //        vendor/bshaffer/oauth2-server-php/src/OAuth2/Storage/Pdo.php
      //    for more info.
      return true;
    }

    public function getDefaultScope($client_id=null) {
      // Do not REQUIRE a scope
      //    see vendor/bshaffer/oauth2-server-php/src/OAuth2/Storage/ScopeInterface.php and
      //    for more info.
      return null;
    }

    public function getUserClaims ($user_id, $claims) {
      // Populate the CLAIMS requested (if any).
      // @TODO: create a more reasonable/comprehensive list.
      // @TODO: present claims on the AUTHORIZATION screen

        $userClaims = Array();
        $claims = explode (' ', trim($claims));
        $validclaims = Array ("name","preferred_username","webfinger","portable_id","email","picture","firstName","lastName");
        $claimsmap = Array (
                            "webfinger" => 'webfinger',
                            "portable_id" => 'portable_id',
                            "name" => 'name',
							"email" => 'email',
                            "preferred_username" => 'username',
							"picture" => 'picture',
							"given_name" => 'firstName',
							"family_name" => 'lastName'
                           );
        $userinfo = $this->getUser($user_id);
        foreach ($validclaims as $validclaim) {
            if (in_array($validclaim,$claims)) {
              $claimkey = $claimsmap[$validclaim];
              $userClaims[$validclaim] = $userinfo[$claimkey];
            } else {
              $userClaims[$validclaim] = $validclaim;
            }
        }
        $userClaims["sub"]=$user_id;
        return $userClaims; 
    }

    /**
     * plaintext passwords are bad!  Override this for your application
     *
     * @param string $username
     * @param string $password
     * @param string $firstName
     * @param string $lastName
     * @return bool
     */
    public function setUser($username, $password, $firstName = null, $lastName = null)
    {
        return true;
    }

}