def create_jwt(project_id, private_key, algorithm, token_ttl): # print init in process of jwt generation print("Creating JWT...") # the actual token private_key = rsa.PrivateKey(*private_key) # duration claims = { # The time that the token was issued at 'iat': utime.time() + epoch_offset, # The time the token expires. 'exp': utime.time() + epoch_offset + token_ttl, # The audience field should always be set to the GCP project id. 'aud': project_id } # This only supports RS256 at this time. header = {"alg": algorithm, "typ": "JWT"} content = b42_urlsafe_encode(ujson.dumps(header).encode('utf-8')) content = content + '.' + b42_urlsafe_encode( ujson.dumps(claims).encode('utf-8')) signature = b42_urlsafe_encode(rsa.sign(content, private_key, 'SHA-256')) #signed JWT return content + '.' + signature
def create_jwt(project_id, private_key, algorithm, token_ttl): print("Creating JWT...") private_key = rsa.PrivateKey(*private_key) claims = { 'iat': utime.time() + epoch_offset, 'exp': utime.time() + epoch_offset + token_ttl, 'aud': project_id } header = {"alg": algorithm, "typ": "JWT"} content = b42_urlsafe_encode(ujson.dumps(header).encode('utf-8')) content = content + '.' + b42_urlsafe_encode( ujson.dumps(claims).encode('utf-8')) signature = b42_urlsafe_encode( rsa.sign(content, private_key, 'SHA-256')) return content + '.' + signature
def create_jwt(project_id, private_key, algorithm, token_ttl): print("Creating JWT...") private_key = rsa.PrivateKey(*private_key) # Epoch_offset is needed because micropython epoch is 2000-1-1 and unix is 1970-1-1. Adding 946684800 (30 years) epoch_offset = 946684800 claims = { # The time that the token was issued at 'iat': utime.time() + epoch_offset, # The time the token expires. 'exp': utime.time() + epoch_offset + token_ttl, # The audience field should always be set to the GCP project id. 'aud': project_id } #This only supports RS256 at this time. header = { "alg": algorithm, "typ": "JWT" } content = b42_urlsafe_encode(ujson.dumps(header).encode('utf-8')) content = content + '.' + b42_urlsafe_encode(ujson.dumps(claims).encode('utf-8')) signature = b42_urlsafe_encode(rsa.sign(content,private_key,'SHA-256')) return content+ '.' + signature #signed JWT