<s> import six <EOL> from oslo_config import cfg <EOL> from st2common . util import isotime <EOL> from st2common . models . api . base import BaseAPI <EOL> from st2common . models . db . auth import UserDB , TokenDB , ApiKeyDB <EOL> from st2common import log as logging <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def get_system_username ( ) : <EOL> return cfg . CONF . system_user . user <EOL> class UserAPI ( BaseAPI ) : <EOL> model = UserDB <EOL> schema = { <EOL> "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" , <EOL> "<STR_LIT>" : True <EOL> } <EOL> } , <EOL> "<STR_LIT>" : False <EOL> } <EOL> @ classmethod <EOL> def to_model ( cls , user ) : <EOL> name = user . name <EOL> model = cls . model ( name = name ) <EOL> return model <EOL> class TokenAPI ( BaseAPI ) : <EOL> model = TokenDB <EOL> schema = { <EOL> "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:id>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT:user>" : { <EOL> "<STR_LIT:type>" : [ "<STR_LIT:string>" , "<STR_LIT:null>" ] <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : [ "<STR_LIT:string>" , "<STR_LIT:null>" ] <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:1> <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : [ "<STR_LIT:string>" , "<STR_LIT:null>" ] , <EOL> "<STR_LIT>" : isotime . ISO8601_UTC_REGEX <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : [ "<STR_LIT:object>" , "<STR_LIT:null>" ] <EOL> } <EOL> } , <EOL> "<STR_LIT>" : False <EOL> } <EOL> @ classmethod <EOL> def from_model ( cls , model , mask_secrets = False ) : <EOL> doc = super ( cls , cls ) . _from_model ( model , mask_secrets = mask_secrets ) <EOL> doc [ '<STR_LIT>' ] = isotime . format ( model . expiry , offset = False ) if model . expiry else None <EOL> return cls ( ** doc ) <EOL> @ classmethod <EOL> def to_model ( cls , instance ) : <EOL> user = str ( instance . user ) if instance . user else None <EOL> token = str ( instance . token ) if instance . token else None <EOL> expiry = isotime . parse ( instance . expiry ) if instance . expiry else None <EOL> model = cls . model ( user = user , token = token , expiry = expiry ) <EOL> return model <EOL> class ApiKeyAPI ( BaseAPI ) : <EOL> model = ApiKeyDB <EOL> schema = { <EOL> "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:id>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT:user>" : { <EOL> "<STR_LIT:type>" : [ "<STR_LIT:string>" , "<STR_LIT:null>" ] , <EOL> "<STR_LIT:default>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : [ "<STR_LIT:string>" , "<STR_LIT:null>" ] <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : [ "<STR_LIT:object>" , "<STR_LIT:null>" ] <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : isotime . ISO8601_UTC_REGEX <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:description>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:default>" : True <EOL> } <EOL> } , <EOL> "<STR_LIT>" : False <EOL> } <EOL> @ classmethod <EOL> def from_model ( cls , model , mask_secrets = False ) : <EOL> doc = super ( cls , cls ) . _from_model ( model , mask_secrets = mask_secrets ) <EOL> doc [ '<STR_LIT>' ] = isotime . format ( model . created_at , offset = False ) if model . created_at else None <EOL> return cls ( ** doc ) <EOL> @ classmethod <EOL> def to_model ( cls , instance ) : <EOL> user = str ( instance . user ) if instance . user else None <EOL> key_hash = getattr ( instance , '<STR_LIT>' , None ) <EOL> metadata = getattr ( instance , '<STR_LIT>' , { } ) <EOL> enabled = bool ( getattr ( instance , '<STR_LIT>' , True ) ) <EOL> model = cls . model ( user = user , key_hash = key_hash , metadata = metadata , enabled = enabled ) <EOL> return model <EOL> class ApiKeyCreateResponseAPI ( BaseAPI ) : <EOL> schema = { <EOL> "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:id>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT:user>" : { <EOL> "<STR_LIT:type>" : [ "<STR_LIT:string>" , "<STR_LIT:null>" ] , <EOL> "<STR_LIT:default>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT:key>" : { <EOL> "<STR_LIT:type>" : [ "<STR_LIT:string>" , "<STR_LIT:null>" ] <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : [ "<STR_LIT:object>" , "<STR_LIT:null>" ] <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : isotime . ISO8601_UTC_REGEX <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:description>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:default>" : True <EOL> } <EOL> } , <EOL> "<STR_LIT>" : False <EOL> } <EOL> @ classmethod <EOL> def from_model ( cls , model , mask_secrets = False ) : <EOL> doc = cls . _from_model ( model = model , mask_secrets = mask_secrets ) <EOL> attrs = { attr : value for attr , value in six . iteritems ( doc ) if value is not None } <EOL> attrs [ '<STR_LIT>' ] = isotime . format ( model . created_at , offset = False ) if model . created_at else None <EOL> attrs . pop ( '<STR_LIT>' , None ) <EOL> attrs [ '<STR_LIT:key>' ] = None <EOL> return cls ( ** attrs ) </s>
