MCPcopy Create free account
hub / github.com/StackStorm/st2 / post

Method post

st2api/st2api/controllers/v1/auth.py:129–180  ·  view source on GitHub ↗

Create a new entry.

(self, api_key_api, requester_user)

Source from the content-addressed store, hash-verified

127 return resp
128
129 def post(self, api_key_api, requester_user):
130 """
131 Create a new entry.
132 """
133
134 permission_type = PermissionType.API_KEY_CREATE
135 rbac_utils = get_rbac_backend().get_utils_class()
136 rbac_utils.assert_user_has_resource_api_permission(
137 user_db=requester_user,
138 resource_api=api_key_api,
139 permission_type=permission_type,
140 )
141
142 api_key_db = None
143 api_key = None
144 try:
145 if not getattr(api_key_api, "user", None):
146 if requester_user:
147 api_key_api.user = requester_user.name
148 else:
149 api_key_api.user = cfg.CONF.system_user.user
150
151 try:
152 User.get_by_name(api_key_api.user)
153 except StackStormDBObjectNotFoundError:
154 user_db = UserDB(name=api_key_api.user)
155 User.add_or_update(user_db)
156
157 extra = {"username": api_key_api.user, "user": user_db}
158 LOG.audit('Registered new user "%s".' % (api_key_api.user), extra=extra)
159
160 # If key_hash is provided use that and do not create a new key. The assumption
161 # is user already has the original api-key
162 if not getattr(api_key_api, "key_hash", None):
163 api_key, api_key_hash = auth_util.generate_api_key_and_hash()
164 # store key_hash in DB
165 api_key_api.key_hash = api_key_hash
166 api_key_db = ApiKey.add_or_update(ApiKeyAPI.to_model(api_key_api))
167 except (ValidationError, ValueError) as e:
168 LOG.exception("Validation failed for api_key data=%s.", api_key_api)
169 abort(http_client.BAD_REQUEST, six.text_type(e))
170
171 extra = {"api_key_db": api_key_db}
172 LOG.audit("ApiKey created. ApiKey.id=%s" % (api_key_db.id), extra=extra)
173
174 api_key_create_response_api = ApiKeyCreateResponseAPI.from_model(api_key_db)
175 # Return real api_key back to user. A one-way hash of the api_key is stored in the DB
176 # only the real value only returned at create time. Also, no masking of key here since
177 # the user needs to see this value atleast once.
178 api_key_create_response_api.key = api_key
179
180 return Response(json=api_key_create_response_api, status=http_client.CREATED)
181
182 def put(self, api_key_api, api_key_id_or_key, requester_user):
183 api_key_db = ApiKey.get_by_key_or_id(api_key_id_or_key)

Callers

nothing calls this directly

Calls 10

get_rbac_backendFunction · 0.90
UserDBClass · 0.90
abortFunction · 0.90
ResponseClass · 0.90
get_utils_classMethod · 0.45
get_by_nameMethod · 0.45
add_or_updateMethod · 0.45
to_modelMethod · 0.45
from_modelMethod · 0.45

Tested by

no test coverage detected