async def store( form: ApiTokenCreateForm, api_token_repo: ApiTokenRepo = Depends(ApiTokenRepo()), dns_server_repo: DnsServerRepo = Depends(DnsServerRepo()), http_server_repo: HttpServerRepo = Depends(HttpServerRepo()), token: TokenPayload = Depends(ScopedTo("api-token:create")), user: User = Depends(current_user), ): if form.dns_server_id and form.dns_server_id > 0: dns_server_name = (dns_server_repo.first_or_fail( id=form.dns_server_id).results().name) if form.http_server_id and form.http_server_id > 0: http_server_name = (http_server_repo.first_or_fail( id=form.http_server_id).results().name) scopes = [] for requested_scope in form.scopes.split(" "): request_scope_satisfied = False for user_token in token.scopes: # TODO: double check this, pretty lenient # if a:b in a:b:c if user_token in requested_scope: request_scope_satisfied = True if not request_scope_satisfied: logger.warning( f"[email protected]: Attempt to create unauthorized scope {requested_scope}" ) raise HTTPException(403, detail="unauthorized") else: scopes.append(requested_scope) # TODO: use better randomness token = create_bearer_token( data={ "sub": user.id, "scopes": " ".join(scopes), "dns_server_name": dns_server_name, "http_server_name": http_server_name, }) data = { "scopes": " ".join(scopes), "token": str(token), "expires_at": form.expires_at, "dns_server_id": form.dns_server_id, "http_server_id": form.http_server_id, } api_token = api_token_repo.create(data).data() return ApiTokenResponse(api_token=api_token)
def create_or_get_api_token_for_dns_server( token: TokenPayload, api_token_repo: ApiTokenRepo, dns_server: DnsServer) -> ApiTokenData: scopes = token.scopes if not api_token_repo.exists(token=token.token): api_token_repo.clear() logger.info( "[email protected] - Saving api token from auth token for dns node") return api_token_repo.create( dict( token=token.token, scopes=" ".join(scopes), dns_server=dns_server, expires_at=datetime.utcfromtimestamp(float(token.exp)), )).data() else: logger.info("[email protected] - token already exists in database") return api_token_repo.loads("dns_server").includes("dns_server").data()