-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
73 lines (68 loc) · 1.82 KB
/
auth.js
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
import axios from 'axios'
import variables from './variables.js'
const authAjax = axios.create({
baseURL: 'https://ca.account.sony.com/api/authz/v3/',
timeout: 15000,
})
// https://ca.account.sony.com/api/v1/ssocookie
async function getCode() {
var config = {
method: 'get',
url: '/oauth/authorize',
headers: {
Cookie: 'npsso=' + variables.NPSSO,
},
params: {
access_type: 'offline',
client_id: '09515159-7237-4370-9b40-3806e67c0891',
redirect_uri: 'com.scee.psxandroid.scecompcall://redirect',
response_type: 'code',
scope: 'psn:mobile.v2.core psn:clientapp',
},
maxRedirects: 0,
}
try {
await authAjax(config)
} catch (err) {
if (err.response) {
if (err.response.headers.location) {
console.log(err.response.headers.location)
const resUrl = new URL(err.response.headers.location)
return resUrl.searchParams.get('code')
} else {
throw new Error(JSON.stringify(err.response))
}
} else {
return null
}
}
}
async function getToken(code) {
var data = new URLSearchParams({
code,
redirect_uri: 'com.scee.psxandroid.scecompcall://redirect',
grant_type: 'authorization_code',
token_format: 'jwt',
}).toString()
var config = {
method: 'post',
url: '/oauth/token',
headers: {
Authorization:
'Basic MDk1MTUxNTktNzIzNy00MzcwLTliNDAtMzgwNmU2N2MwODkxOnVjUGprYTV0bnRCMktxc1A=',
'Content-Type': 'application/x-www-form-urlencoded',
},
data,
}
const response = await authAjax(config)
return response.data.access_token
}
export default async function auth() {
console.log('getCode')
const code = await getCode()
console.log('got Code', code)
if (!code) return null
console.log('getToken')
const token = await getToken(code)
return token
}