Skip to content

Instantly share code, notes, and snippets.

@salrashid123
salrashid123 / svc_acct_init.py
Created March 30, 2018 04:46
svc_acct_init
import httplib2
from apiclient.discovery import build
from oauth2client.client import GoogleCredentials
scope = 'https://www.googleapis.com/auth/iam https://www.googleapis.com/auth/cloud-platform'
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "ServiceAccountA_keyFile.json"
credentials = GoogleCredentials.get_application_default()
if credentials.create_scoped_required():
credentials = credentials.create_scoped(scope)
http = credentials.authorize(httplib2.Http())
{
"iss": "serviceAccountB_ID",
"scope": "scope1 scope2",
"aud": "https://accounts.google.com/o/oauth2/token",
"exp": expiration_time,
"iat": issue_time
}
@salrashid123
salrashid123 / svc_sign.py
Created March 30, 2018 04:48
svc_sign.py
client_id= 'serviceAccountB_ID'
slist = resource.serviceAccounts().signJwt(name='projects/mineral-minutia-820/serviceAccounts/' + client_id,
body={'payload': claim })
resp = slist.execute()
signed_jwt = resp['signedJwt']
@salrashid123
salrashid123 / svc_exchange.py
Created March 30, 2018 04:50
svc_exchange.py
url = 'https://accounts.google.com/o/oauth2/token'
data = {'grant_type' : 'assertion',
'assertion_type' : 'http://oauth.net/grant_type/jwt/1.0/bearer',
'assertion' : signed_jwt }
headers = {"Content-type": "application/x-www-form-urlencoded"}
data = urllib.urlencode(data)
req = urllib2.Request(url, data, headers)
resp = urllib2.urlopen(req).read()
parsed = json.loads(resp)
@salrashid123
salrashid123 / svc_verify_access
Created March 30, 2018 04:51
svc_verify_access
curl https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=ya29.ElnUAws0MfV3om_tqQ0ZS3g7-BZ2ZS_uaJUNdPk8TI7hHkPnFb8Kesg67WQwJswTfD1nYTk-tD0MDUf6X3-pKklpicBDZszq16Q7smBZoDemnYcWaYvJ8gjqnw
{
"azp": "104943293997971332223",
"aud": "104943293997971332223",
"scope": "https://www.googleapis.com/auth/userinfo.email",
"exp": "1484452560",
"expires_in": "3543",
"email": "[email protected]",
"email_verified": "true",
"access_type": "offline"
{
"iss": "serviceAccountB_ID",
"scope": "serviceAccountB_ID",
"aud": "https://www.googleapis.com/oauth2/v4/token",
"exp": expiration_time,
"iat": issue_time
}
client_id= 'serviceAccountB_ID'
slist = resource.serviceAccounts().signJwt(name='projects/mineral-minutia-820/serviceAccounts/' + client_id,
body={'payload': id_token_claim })
resp = slist.execute()
signed_jwt = resp['signedJwt']
url = 'https://www.googleapis.com/oauth2/v4/token'
data = {'grant_type' : 'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion' : signed_jwt }
headers = {"Content-type": "application/x-www-form-urlencoded"}
data = urllib.urlencode(data)
req = urllib2.Request(url, data, headers)
resp = urllib2.urlopen(req).read()
parsed = json.loads(resp)
id_token = parsed.get('id_token')
@salrashid123
salrashid123 / svc3.py
Created March 30, 2018 05:05
svc3.py
from oauth2client.client import verify_id_token
from oauth2client.crypt import AppIdentityError
try:
jwt = verify_id_token(id_token, client_id)
self.log('\n ID_TOKEN Validation: \n ' + json.dumps(jwt,sort_keys = False, indent = 4) +' \n', logging.INFO)
except AppIdentityError, e:
self.log('Payload: ' + str(e.read), logging.ERROR)
@salrashid123
salrashid123 / svc4.py
Created March 30, 2018 05:07
svc4.py
audience = 'SystemC'
id_scope='scope1 scope2'
now = int(time.time())
exptime = now + 3600
id_token_claim =('{"iss":"%s","scope":"%s", "aud":"%s","exp":%s,"iat":%s}') %(client_id,id_scope,audience,exptime,now)
slist = resource.serviceAccounts().signJwt(name='projects/mineral-minutia-820/serviceAccounts/' + client_id,
body={'payload': id_token_claim })
resp = slist.execute()
signed_jwt = resp['signedJwt']