From 61f101ebca8b351bc46619cfd0e67e8639111b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bl=C3=B6cher=20Johanna=20Ruth?= Date: Wed, 24 Jun 2020 15:39:30 +0200 Subject: [PATCH] Added code challenger and code verification to ee authorisation routine. --- R/auth.R | 8 ++++---- inst/Python/ee_authorisation_function.py | 26 ++++++++++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/R/auth.R b/R/auth.R index 35eb1a6..82f02d3 100755 --- a/R/auth.R +++ b/R/auth.R @@ -35,20 +35,20 @@ run_ee_oauth <- function() { load_test <- try(source_python(file = oauth_func_path), silent = T) count <- count + 1 } - request_ee_code() + verify <- request_ee_code() code <- readline("Enter authorisation code for Earth Engine API here: ") - test <- try(request_ee_token(code), silent = T) + test <- try(request_ee_token(code, verify), silent = T) while (class(test) == "try-error") { cat( "Problem with Authentication key input. \nPlease follow the authentication steps in the browser and copy paste the authentication token into the R console again." ) - request_ee_code() + verify <- request_ee_code() code <- readline("enter authorisation code here: ") - test <- try(request_ee_token(code), silent = T) + test <- try(request_ee_token(code, verify), silent = T) } cat("Earth Engine Python API is authenticated \n") } diff --git a/inst/Python/ee_authorisation_function.py b/inst/Python/ee_authorisation_function.py index 53953da..bb8d336 100755 --- a/inst/Python/ee_authorisation_function.py +++ b/inst/Python/ee_authorisation_function.py @@ -4,20 +4,38 @@ import urllib from ee.oauth import get_credentials_path import json +import os +import base64 +import hashlib ft_scope = 'https://www.googleapis.com/auth/fusiontables' def request_ee_code(): - # get authorisation url - auth_url = ee.oauth.get_authorization_url() + out = create_codes() + code_verifier = out[0] + code_challenge = out[1] + # get authorisation url + auth_url = ee.oauth.get_authorization_url(code_challenge) # call auth_url in browser to grand access by the user webbrowser.open_new(auth_url) + return code_verifier -def request_ee_token(auth_code): - token = ee.oauth.request_token(auth_code) +def request_ee_token(auth_code, code_verifier): + token = ee.oauth.request_token(auth_code, code_verifier) ee.oauth.write_token(token) + +def _base64param(byte_string): + """Encodes bytes for use as a URL parameter.""" + return base64.urlsafe_b64encode(byte_string).rstrip(b'=') + +def create_codes(): + code_verifier = _base64param(os.urandom(32)) + code_challenge = _base64param(hashlib.sha256(code_verifier).digest()) + return code_verifier, code_challenge + +