ue) == 1: value = value[0] d[key] = value response = jsonify(d) for key, value in headers.items(multi=True): response.headers.add(key, value) response_has_changed = response.data != original_data if not response_has_changed: break return response @app.route("/cookies") def view_cookies(hide_env=True): """Returns cookie data. --- tags: - Cookies produces: - application/json responses: 200: description: Set cookies. """ cookies = dict(request.cookies.items()) if hide_env and ("show_env" not in request.args): for key in ENV_COOKIES: try: del cookies[key] except KeyError: pass return jsonify(cookies=cookies) @app.route("/forms/post") def view_forms_post(): """Simple HTML form.""" return render_template("forms-post.html") @app.route("/cookies/set//") def set_cookie(name, value): """Sets a cookie and redirects to cookie list. --- tags: - Cookies parameters: - in: path name: name type: string - in: path name: value type: string produces: - text/plain responses: 200: description: Set cookies and redirects to cookie list. """ r = app.make_response(redirect(url_for("view_cookies"))) r.set_cookie(key=name, value=value, secure=secure_cookie()) return r @app.route("/cookies/set") def set_cookies(): """Sets cookie(s) as provided by the query string and redirects to cookie list. --- tags: - Cookies parameters: - in: query name: freeform explode: true allowEmptyValue: true schema: type: object additionalProperties: type: string style: form produces: - text/plain responses: 200: description: Redirect to cookie list """ cookies = dict(request.args.items()) r = app.make_response(redirect(url_for("view_cookies"))) for key, value in cookies.items(): r.set_cookie(key=key, value=value, secure=secure_cookie()) return r @app.route("/cookies/delete") def delete_cookies(): """Deletes cookie(s) as provided by the query string and redirects to cookie list. --- tags: - Cookies parameters: - in: query name: freeform explode: true allowEmptyValue: true schema: type: object additionalProperties: type: string style: form produces: - text/plain responses: 200: description: Redirect to cookie list """ cookies = dict(request.args.items()) r = app.make_response(redirect(url_for("view_cookies"))) for key, value in cookies.items(): r.delete_cookie(key=key) return r @app.route("/basic-auth//") def basic_auth(user="user", passwd="passwd"): """Prompts the user for authorization using HTTP Basic Auth. --- tags: - Auth parameters: - in: path name: user type: string - in: path name: passwd type: string produces: - application/json responses: 200: description: Sucessful authentication. 401: description: Unsuccessful authentication. """ if not check_basic_auth(user, passwd): return status_code(401) return jsonify(authenticated=True, user=user) @app.route("/hidden-basic-auth//") def hidden_basic_auth(user="user", passwd="passwd"): """Prompts the user for authorization using HTTP Basic Auth. --- tags: - Auth parameters: - in: path name: user type: string - in: path name: passwd type: string produces: - application/json responses: 200: description: Sucessful authentication. 404: description: Unsuccessful authentication. """ if not check_basic_auth(user, passwd): return status_code(404) return jsonify(authenticated=True, user=user) @app.route("/bearer") def bearer_auth(): """Prompts the user for authorization using bearer authentication. --- tags: - Auth parameters: - in: header name: Authorization schema: type: string produces: - application/json responses: 200: description: Sucessful authentication. 401: description: Unsuccessful authentication. """ authorization = request.headers.get("Authorization") if not (authorization and authorization.startswith("Bearer ")): response = app.make_response("") response.headers["WWW-Authenticate"] = "Bearer" response.status_code = 401 return response slice_start = len("Bearer ") token = authorization[slice_start:] return jsonify(authenticated=True, token=token) @app.route("/digest-auth///") def digest_auth_md5(qop=None, user="user", passwd="passwd"): """Prompts the user for authorization using Digest Auth. --- tags: - Auth parameters: - in: path name: qop type: string description: auth or auth-int - in: path name: user type: string - in: path name: passwd type: string produces: - application/json responses: 200: description: Sucessful authentication. 401: description: Unsuccessful authentication. """ return digest_auth(qop, user, passwd, "MD5", "never") @app.route("/digest-auth////") def digest_auth_nostale(qop=None, user="user", passwd="passwd", algorithm="MD5"): """Prompts the user for authorization using Digest Auth + Algorithm. --- tags: - Auth parameters: - in: path name: qop type: string description: auth or auth-int - in: path name: user type: string - in: path name: passwd type: string - in: path name: algorithm type: string description: MD5, SHA-256, SHA-512 default: MD5 produces: - application/json responses: 200: description: Sucessful authentication. 401: description: Unsuccessful authentication. """ return digest_auth(qop, user, passwd, algorithm, "never") @app.route("/digest-auth/////") def digest_auth( qop=None, user="user", passwd="passwd", algorithm="MD5", stale_after="never" ): """Prompts the user for authorization using Digest Auth + Algorithm. allow settings the stale_after argument. --- tags: - Auth parameters: - in: path name: qop type: string description: auth or auth-int - in: path name: user type: string - in: path name: passwd type: string - in: path name: algorithm type: string description: MD5, SHA-256, SHA-512 default: MD5 - in: path name: stale_after type: string default: never produces: - application/json responses: 200: description: Sucessful authentication. 401: description: Unsuccessful authentication. """ require_cookie_handling = request.args.get("require-cookie", "").lower() in ( "1", "t", "true", ) if algorithm not in ("MD5", "SHA-256", "SHA-512"): algorithm = "MD5" if qop not in ("auth", "auth-int"): qop = None authorization = request.headers.get("Authorization") credentials = None if authorization: credentials = parse_authorization_header(authorization) if ( not authorization or not credentials or credentials.type.lower() != "digest" or (require_cookie_handling and "Cookie" not in request.headers) ): response = digest_challenge_response(app, qop, algorithm) response.set_cookie("stale_after", value=stale_after) response.set_cookie("fake", value="fake_value") return response if require_cookie_handling and request.cookies.get("fake") != "fake_value": response = jsonify({"errors": ["missing cookie set on challenge"]}) response.set_cookie("fake", value="fake_value") response.status_code = 403 return response current_nonce = credentials.get("nonce") stale_after_value = None if "stale_after" in request.cookies: stale_after_value = request.cookies.get("stale_after") if ( "last_nonce" in request.cookies and current_nonce == request.cookies.get("last_nonce") or stale_after_value == "0" ): response = digest_challenge_response(app, qop, algorithm, True) response.set_cookie("stale_after", value=stale_after) response.set_cookie("last_nonce", value=current_nonce) response.set_cookie("fake", value="fake_value") return response if not check_digest_auth(user, passwd): response = digest_challenge_response(app, qop, algorithm, False) response.set_cookie("stale_after", value=stale_after) response.set_cookie("last_nonce", value=current_nonce) response.set_cookie("fake", value="fake_value") return response response = jsonify(authenticated=True, user=user) response.set_cookie("fake", value="fake_value") if stale_after_value: response.set_cookie( "stale_after", value=next_stale_after_value(stale_after_value)