AuthenticationEndpoints   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 33
c 1
b 0
f 0
dl 0
loc 132
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A authRequest() 0 11 1
A entitlement() 0 3 1
A multiFactorAuthentication() 0 9 1
A playerInfo() 0 3 1
A reauthCookie() 0 16 2
A parseTokenURL() 0 11 2
A authCookies() 0 11 2
1
<?php
2
3
namespace Seaony\ValorantApi\Endpoints;
4
5
use Illuminate\Support\Arr;
6
7
trait AuthenticationEndpoints
8
{
9
    /**
10
     * Prepare cookies for auth request
11
     *
12
     * @return false|mixed
13
     * @throws \GuzzleHttp\Exception\GuzzleException
14
     */
15
    public function authCookies($params = [])
16
    {
17
        $params = $params ?: [
18
            'client_id'     => 'play-valorant-web-prod',
19
            'nonce'         => '1',
20
            'redirect_uri'  => 'https://playvalorant.com/opt_in',
21
            'response_type' => 'token id_token',
22
            'scope'         => 'account openid',
23
        ];
24
25
        return $this->request('POST', 'https://auth.riotgames.com/api/v1/authorization', ['json' => $params]);
0 ignored issues
show
Bug introduced by
It seems like request() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        return $this->/** @scrutinizer ignore-call */ request('POST', 'https://auth.riotgames.com/api/v1/authorization', ['json' => $params]);
Loading history...
26
    }
27
28
    /**
29
     * Perform authorization request to get token
30
     * Requires cookies from the Auth Cookies stage. The token can be found in the uri property.
31
     *
32
     * @param  string  $username  Riot Username
33
     * @param  string  $password  Riot Password
34
     * @param $remember
35
     *
36
     * @return false|mixed
37
     * @throws \GuzzleHttp\Exception\GuzzleException
38
     */
39
    public function authRequest(string $username, string $password, $remember = true)
40
    {
41
        $params = [
42
            'type'     => 'auth',
43
            'username' => $username,
44
            'password' => $password,
45
            'remember' => $remember,
46
            'language' => 'en_US',
47
        ];
48
49
        return $this->request('PUT', 'https://auth.riotgames.com/api/v1/authorization', ['json' => $params]);
50
    }
51
52
    /**
53
     * Get a new token using the cookies from a previous
54
     * authorization request Use the saved cookies from Auth Request
55
     * (specifically the ssid cookie). The token can be found from the url
56
     * this request redirects to. Recommended to use this endpoint instead of
57
     * storing the password and sending it again.
58
     *
59
     * @return false|mixed
60
     * @throws \GuzzleHttp\Exception\GuzzleException
61
     */
62
    public function reauthCookie()
63
    {
64
        $reauthURL = 'https://auth.riotgames.com/authorize?redirect_uri=https%3A%2F%2Fplayvalorant.com%2Fopt_in&client_id=play-valorant-web-prod&response_type=token%20id_token&nonce=1';
65
66
        $response = $this->request('GET', $reauthURL, ['allow_redirects' => false], false);
67
68
        // 从响应头中获取 location
69
        $location = $response->getHeader('location');
70
71
        // 如果没有 location 则返回 false
72
        if (!count($location)) {
73
            return false;
74
        }
75
76
        // 返回 Token
77
        return $this->parseTokenURL($location[0]);
78
    }
79
80
    /**
81
     * Get entitlement for remote requests with a token
82
     *
83
     * @return void
84
     * @throws \GuzzleHttp\Exception\GuzzleException
85
     */
86
    public function entitlement()
87
    {
88
        return $this->request('POST', 'https://entitlements.auth.riotgames.com/api/token/v1');
89
    }
90
91
    /**
92
     * https://auth.riotgames.com/api/v1/authorization
93
     *
94
     * @param $code Multi-Factor Authentication Code
0 ignored issues
show
Documentation Bug introduced by
The doc comment Multi-Factor at position 0 could not be parsed: Unknown type name 'Multi-Factor' at position 0 in Multi-Factor.
Loading history...
95
     *
96
     * @return false|mixed
97
     * @throws \GuzzleHttp\Exception\GuzzleException
98
     */
99
    public function multiFactorAuthentication($code, $rememberDevice = true)
100
    {
101
        $params = [
102
            'type'           => 'multifactor',
103
            'code'           => $code,
104
            'rememberDevice' => $rememberDevice,
105
        ];
106
107
        return $this->request('PUT', 'https://auth.riotgames.com/api/v1/authorization', ['json' => $params]);
108
    }
109
110
    /**
111
     * Get the PUUID and other info from a token
112
     *
113
     * @return false|mixed
114
     * @throws \GuzzleHttp\Exception\GuzzleException
115
     */
116
    public function playerInfo()
117
    {
118
        return $this->request('GET', 'https://auth.riotgames.com/userinfo');
119
    }
120
121
    /**
122
     * Parse token from auth url
123
     *
124
     * @param $url string Riot auth url
125
     *
126
     * @return array
127
     */
128
    public function parseTokenURL(string $url)
129
    {
130
        $parsed = parse_url($url);
131
132
        if (!isset($parsed['fragment'])) {
133
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
134
        }
135
136
        parse_str($parsed['fragment'], $params);
137
138
        return $params;
139
    }
140
}
141