<s> import base64 <EOL> import datetime <EOL> import json <EOL> import logging <EOL> from oslo . config import cfg <EOL> import SoftLayer <EOL> from jumpgate . common import aes <EOL> from jumpgate . common . sl import auth <EOL> from jumpgate . identity . drivers import core as identity <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def parse_templates ( template_lines ) : <EOL> o = { } <EOL> for line in template_lines : <EOL> if '<STR_LIT>' not in line : <EOL> continue <EOL> k , v = line . strip ( ) . split ( '<STR_LIT>' ) <EOL> if not k . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> parts = k . split ( '<STR_LIT:.>' ) <EOL> region , service , key = parts [ <NUM_LIT:1> : <NUM_LIT:4> ] <EOL> region_ref = o . get ( region , { } ) <EOL> service_ref = region_ref . get ( service , { } ) <EOL> service_ref [ key ] = v <EOL> region_ref [ service ] = service_ref <EOL> o [ region ] = region_ref <EOL> return o <EOL> def get_access ( token_id , token_details , user ) : <EOL> return { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : datetime . datetime . fromtimestamp ( <EOL> token_details [ '<STR_LIT>' ] ) . strftime ( '<STR_LIT>' ) , <EOL> '<STR_LIT:id>' : token_id , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : token_details [ '<STR_LIT>' ] , <EOL> '<STR_LIT:name>' : token_details [ '<STR_LIT>' ] , <EOL> } , <EOL> } , <EOL> '<STR_LIT:user>' : { <EOL> '<STR_LIT:username>' : user [ '<STR_LIT:username>' ] , <EOL> '<STR_LIT:id>' : user [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT:name>' : '<STR_LIT:user>' } , <EOL> ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:name>' : user [ '<STR_LIT:username>' ] , <EOL> } , <EOL> } <EOL> def get_access_v3 ( token_id , token_details , user_id , user_name ) : <EOL> return { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : datetime . datetime . fromtimestamp ( <EOL> token_details [ '<STR_LIT>' ] ) . isoformat ( ) , <EOL> '<STR_LIT>' : datetime . datetime . fromtimestamp ( <EOL> token_details [ '<STR_LIT>' ] ) . isoformat ( ) , <EOL> '<STR_LIT>' : [ '<STR_LIT:password>' ] , <EOL> '<STR_LIT:id>' : token_id , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : token_details [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> ] , <EOL> '<STR_LIT:name>' : token_details [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : '<STR_LIT:default>' , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> ] , <EOL> '<STR_LIT:name>' : '<STR_LIT>' } <EOL> } , <EOL> '<STR_LIT:user>' : { <EOL> '<STR_LIT:id>' : user_id , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> ] , <EOL> '<STR_LIT:name>' : user_name , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : '<STR_LIT:default>' , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> ] , <EOL> '<STR_LIT:name>' : '<STR_LIT>' } <EOL> } <EOL> } <EOL> } <EOL> class AuthTokensV3 ( object ) : <EOL> def __init__ ( self , template_file ) : <EOL> self . _load_templates ( template_file ) <EOL> def _load_templates ( self , template_file ) : <EOL> try : <EOL> self . templates = parse_templates ( open ( template_file ) ) <EOL> except IOError : <EOL> LOG . critical ( '<STR_LIT>' , template_file ) <EOL> raise <EOL> def _get_catalog ( self , tenant_id , user_id ) : <EOL> d = { '<STR_LIT>' : tenant_id , '<STR_LIT>' : user_id } <EOL> o = { } <EOL> for region , region_ref in self . templates . items ( ) : <EOL> o [ region ] = { } <EOL> for service , service_ref in region_ref . items ( ) : <EOL> o [ region ] [ service ] = { } <EOL> for k , v in service_ref . items ( ) : <EOL> o [ region ] [ service ] [ k ] = v . replace ( '<STR_LIT>' , '<STR_LIT>' ) % d <EOL> return o <EOL> def _build_catalog ( self , token_details , user_id ) : <EOL> raw_catalog = self . _get_catalog ( token_details [ '<STR_LIT>' ] , user_id ) <EOL> catalog = [ ] <EOL> for services in raw_catalog . values ( ) : <EOL> for service_type , service in services . items ( ) : <EOL> d = { <EOL> '<STR_LIT:type>' : service_type , <EOL> '<STR_LIT:name>' : service . get ( '<STR_LIT:name>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : service . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:url>' : service . get ( '<STR_LIT>' ) <EOL> } , { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : service . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:url>' : service . get ( '<STR_LIT>' ) <EOL> } , { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : service . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:url>' : service . get ( '<STR_LIT>' ) , <EOL> } ] <EOL> } <EOL> catalog . append ( d ) <EOL> return catalog <EOL> def on_post ( self , req , resp ) : <EOL> body = req . stream . read ( ) . decode ( ) <EOL> credentials = json . loads ( body ) <EOL> token_details , user = auth . get_new_token_v3 ( credentials ) <EOL> token_id = base64 . b64encode ( aes . encode_aes ( json . dumps ( token_details ) ) ) <EOL> access = get_access_v3 ( token_id , token_details , <EOL> user [ '<STR_LIT:id>' ] , user [ '<STR_LIT:username>' ] ) <EOL> catalog = self . _build_catalog ( token_details , user [ '<STR_LIT:id>' ] ) <EOL> access [ '<STR_LIT>' ] [ '<STR_LIT>' ] = catalog <EOL> resp . status = <NUM_LIT:200> <EOL> resp . set_header ( '<STR_LIT>' , str ( token_id ) ) <EOL> resp . body = access <EOL> def on_get ( self , req , resp ) : <EOL> toks = [ req . get_header ( '<STR_LIT>' ) , <EOL> req . get_header ( '<STR_LIT>' ) ] <EOL> token_id_driver = identity . token_id_driver ( ) <EOL> tokens = identity . token_driver ( ) <EOL> validated_tokens = [ ] <EOL> for token_id in toks : <EOL> token = token_id_driver . token_from_id ( token_id ) <EOL> tokens . validate_token ( token ) <EOL> validated_tokens . append ( token ) <EOL> access = get_access_v3 ( toks [ <NUM_LIT:1> ] , validated_tokens [ <NUM_LIT:1> ] , <EOL> tokens . user_id ( validated_tokens [ <NUM_LIT:1> ] ) , <EOL> tokens . username ( validated_tokens [ <NUM_LIT:1> ] ) ) <EOL> if '<STR_LIT>' not in req . query_string : <EOL> catalog = self . _build_catalog ( validated_tokens [ <NUM_LIT:1> ] , <EOL> tokens . user_id ( validated_tokens [ <NUM_LIT:1> ] ) ) <EOL> access [ '<STR_LIT>' ] [ '<STR_LIT>' ] = catalog <EOL> resp . status = <NUM_LIT:200> <EOL> resp . set_header ( '<STR_LIT>' , toks [ <NUM_LIT:0> ] ) <EOL> resp . set_header ( '<STR_LIT>' , toks [ <NUM_LIT:1> ] ) <EOL> resp . body = access <EOL> class TokenV2 ( object ) : <EOL> def on_get ( self , req , resp , token_id ) : <EOL> token_details = auth . get_token_details ( <EOL> token_id , tenant_id = req . get_param ( '<STR_LIT>' ) ) <EOL> endpoint = cfg . CONF [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> client = SoftLayer . Client ( endpoint_url = endpoint ) <EOL> client . auth = auth . get_auth ( token_details ) <EOL> user = client [ '<STR_LIT>' ] . getCurrentUser ( mask = '<STR_LIT>' ) <EOL> access = get_access ( token_id , token_details , user ) <EOL> resp . status = <NUM_LIT:200> <EOL> resp . body = { '<STR_LIT>' : access } <EOL> def on_delete ( self , req , resp , token_id ) : <EOL> LOG . warning ( '<STR_LIT>' , token_id ) <EOL> resp . status = <NUM_LIT> <EOL> resp . body = '<STR_LIT>' </s>
