Use Google 3P authorization

This commit is contained in:
KatieFrogs 2022-03-11 17:34:00 +03:00
parent 27c8526c2a
commit 9c31d5b8a0
2 changed files with 42 additions and 51 deletions

View File

@ -516,6 +516,9 @@ class CustomSongs{
pageEvents.remove(document, ["dragover", "dragleave", "drop"]) pageEvents.remove(document, ["dragover", "dragleave", "drop"])
delete this.dropzone delete this.dropzone
} }
if(gpicker){
gpicker.tokenResolve = null
}
delete this.browse delete this.browse
delete this.linkLocalFolder delete this.linkLocalFolder
delete this.linkGdriveFolder delete this.linkGdriveFolder

View File

@ -9,9 +9,9 @@ class Gpicker{
this.scope = "https://www.googleapis.com/auth/drive.readonly" this.scope = "https://www.googleapis.com/auth/drive.readonly"
this.folder = "application/vnd.google-apps.folder" this.folder = "application/vnd.google-apps.folder"
this.filesUrl = "https://www.googleapis.com/drive/v3/files/" this.filesUrl = "https://www.googleapis.com/drive/v3/files/"
this.discoveryDocs = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"]
this.resolveQueue = [] this.resolveQueue = []
this.queueActive = false this.queueActive = false
this.clientCallbackBind = this.clientCallback.bind(this)
} }
browse(lockedCallback, errorCallback){ browse(lockedCallback, errorCallback){
return this.loadApi() return this.loadApi()
@ -124,9 +124,12 @@ class Gpicker{
if(window.gapi && gapi.client && gapi.client.drive){ if(window.gapi && gapi.client && gapi.client.drive){
return Promise.resolve() return Promise.resolve()
} }
return loader.loadScript("https://apis.google.com/js/api.js") var promises = [
.then(() => new Promise((resolve, reject) => loader.loadScript("https://apis.google.com/js/api.js"),
gapi.load("auth2:picker:client", { loader.loadScript("https://accounts.google.com/gsi/client")
]
return Promise.all(promises).then(() => new Promise((resolve, reject) =>
gapi.load("picker:client", {
callback: resolve, callback: resolve,
onerror: reject onerror: reject
}) })
@ -135,68 +138,53 @@ class Gpicker{
gapi.client.load("drive", "v3").then(resolve, reject) gapi.client.load("drive", "v3").then(resolve, reject)
)) ))
} }
getAuth(errorCallback=()=>{}){ getClient(errorCallback=()=>{}, force){
if(!this.auth){ var obj = {
return new Promise((resolve, reject) => { client_id: this.oauthClientId,
gapi.auth2.init({ scope: this.scope,
apiKey: this.apiKey, callback: this.clientCallbackBind
clientId: this.oauthClientId,
discoveryDocs: this.discoveryDocs,
fetch_basic_profile: false,
scope: this.scope
}).then(() => {
this.auth = gapi.auth2.getAuthInstance()
resolve(this.auth)
}, e => {
if(e.details){
var errorStr = strings.gpicker.authError.replace("%s", e.details)
if(/cookie/i.test(e.details)){
errorStr += "\n\n" + strings.gpicker.cookieError
} }
errorCallback(errorStr) if(force){
if(!this.clientForce){
obj.select_account = true
this.clientForce = google.accounts.oauth2.initTokenClient(obj)
} }
reject(e) return this.clientForce
})
})
}else{ }else{
return Promise.resolve(this.auth) if(!this.client){
this.client = google.accounts.oauth2.initTokenClient(obj)
}
return this.client
}
}
clientCallback(tokenResponse){
this.tokenResponse = tokenResponse
this.oauthToken = tokenResponse.access_token
if(this.oauthToken && this.tokenResolve){
this.tokenResolve()
} }
} }
getToken(lockedCallback=()=>{}, errorCallback=()=>{}, force){ getToken(lockedCallback=()=>{}, errorCallback=()=>{}, force){
if(this.oauthToken && !force){ if(this.oauthToken && !force){
return Promise.resolve() return Promise.resolve()
} }
return this.getAuth(errorCallback).then(auth => { var client = this.getClient(errorCallback, force)
if(!force && auth.isSignedIn.get() && this.checkScope()){ var promise = new Promise(resolve => {
return Promise.resolve() this.tokenResolve = resolve
}else{ })
lockedCallback(false) lockedCallback(false)
return new Promise((resolve, reject) => client.requestAccessToken()
auth.signIn({ return promise.then(() => {
prompt: force ? "select_account" : "consent", this.tokenResolve = null
scope: this.scope
}).then(resolve, reject)
)
}
}).then(() => {
if(this.checkScope()){ if(this.checkScope()){
lockedCallback(true) lockedCallback(true)
}else{ }else{
return Promise.reject("cancel") return Promise.reject("cancel")
} }
}, e => {
console.error(e)
Promise.reject("cancel")
}) })
} }
checkScope(){ checkScope(){
var user = this.auth.currentUser.get() return google.accounts.oauth2.hasGrantedAnyScope(this.tokenResponse, this.scope)
if(user.hasGrantedScopes(this.scope)){
this.oauthToken = user.getAuthResponse(true).access_token
return this.oauthToken
}else{
return false
}
} }
switchAccounts(lockedCallback, errorCallback){ switchAccounts(lockedCallback, errorCallback){
return this.loadApi().then(() => this.getToken(lockedCallback, errorCallback, true)) return this.loadApi().then(() => this.getToken(lockedCallback, errorCallback, true))