|
| 1 | +from functools import wraps |
| 2 | +from typing import cast |
| 3 | + |
| 4 | +import flask_login |
| 5 | +from flask import request |
| 6 | +from flask_restx import Resource, reqparse |
| 7 | +from werkzeug.exceptions import BadRequest, NotFound |
| 8 | + |
| 9 | +from controllers.console.wraps import account_initialization_required, setup_required |
| 10 | +from core.model_runtime.utils.encoders import jsonable_encoder |
| 11 | +from libs.login import login_required |
| 12 | +from models.account import Account |
| 13 | +from models.model import OAuthProviderApp |
| 14 | +from services.oauth_server import OAUTH_ACCESS_TOKEN_EXPIRES_IN, OAuthGrantType, OAuthServerService |
| 15 | + |
| 16 | +from .. import api |
| 17 | + |
| 18 | + |
| 19 | +def oauth_server_client_id_required(view): |
| 20 | + @wraps(view) |
| 21 | + def decorated(*args, **kwargs): |
| 22 | + parser = reqparse.RequestParser() |
| 23 | + parser.add_argument("client_id", type=str, required=True, location="json") |
| 24 | + parsed_args = parser.parse_args() |
| 25 | + client_id = parsed_args.get("client_id") |
| 26 | + if not client_id: |
| 27 | + raise BadRequest("client_id is required") |
| 28 | + |
| 29 | + oauth_provider_app = OAuthServerService.get_oauth_provider_app(client_id) |
| 30 | + if not oauth_provider_app: |
| 31 | + raise NotFound("client_id is invalid") |
| 32 | + |
| 33 | + kwargs["oauth_provider_app"] = oauth_provider_app |
| 34 | + |
| 35 | + return view(*args, **kwargs) |
| 36 | + |
| 37 | + return decorated |
| 38 | + |
| 39 | + |
| 40 | +def oauth_server_access_token_required(view): |
| 41 | + @wraps(view) |
| 42 | + def decorated(*args, **kwargs): |
| 43 | + oauth_provider_app = kwargs.get("oauth_provider_app") |
| 44 | + if not oauth_provider_app or not isinstance(oauth_provider_app, OAuthProviderApp): |
| 45 | + raise BadRequest("Invalid oauth_provider_app") |
| 46 | + |
| 47 | + if not request.headers.get("Authorization"): |
| 48 | + raise BadRequest("Authorization is required") |
| 49 | + |
| 50 | + authorization_header = request.headers.get("Authorization") |
| 51 | + if not authorization_header: |
| 52 | + raise BadRequest("Authorization header is required") |
| 53 | + |
| 54 | + parts = authorization_header.split(" ") |
| 55 | + if len(parts) != 2: |
| 56 | + raise BadRequest("Invalid Authorization header format") |
| 57 | + |
| 58 | + token_type = parts[0] |
| 59 | + if token_type != "Bearer": |
| 60 | + raise BadRequest("token_type is invalid") |
| 61 | + |
| 62 | + access_token = parts[1] |
| 63 | + if not access_token: |
| 64 | + raise BadRequest("access_token is required") |
| 65 | + |
| 66 | + account = OAuthServerService.validate_oauth_access_token(oauth_provider_app.client_id, access_token) |
| 67 | + if not account: |
| 68 | + raise BadRequest("access_token or client_id is invalid") |
| 69 | + |
| 70 | + kwargs["account"] = account |
| 71 | + |
| 72 | + return view(*args, **kwargs) |
| 73 | + |
| 74 | + return decorated |
| 75 | + |
| 76 | + |
| 77 | +class OAuthServerAppApi(Resource): |
| 78 | + @setup_required |
| 79 | + @oauth_server_client_id_required |
| 80 | + def post(self, oauth_provider_app: OAuthProviderApp): |
| 81 | + parser = reqparse.RequestParser() |
| 82 | + parser.add_argument("redirect_uri", type=str, required=True, location="json") |
| 83 | + parsed_args = parser.parse_args() |
| 84 | + redirect_uri = parsed_args.get("redirect_uri") |
| 85 | + |
| 86 | + # check if redirect_uri is valid |
| 87 | + if redirect_uri not in oauth_provider_app.redirect_uris: |
| 88 | + raise BadRequest("redirect_uri is invalid") |
| 89 | + |
| 90 | + return jsonable_encoder( |
| 91 | + { |
| 92 | + "app_icon": oauth_provider_app.app_icon, |
| 93 | + "app_label": oauth_provider_app.app_label, |
| 94 | + "scope": oauth_provider_app.scope, |
| 95 | + } |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +class OAuthServerUserAuthorizeApi(Resource): |
| 100 | + @setup_required |
| 101 | + @login_required |
| 102 | + @account_initialization_required |
| 103 | + @oauth_server_client_id_required |
| 104 | + def post(self, oauth_provider_app: OAuthProviderApp): |
| 105 | + account = cast(Account, flask_login.current_user) |
| 106 | + user_account_id = account.id |
| 107 | + |
| 108 | + code = OAuthServerService.sign_oauth_authorization_code(oauth_provider_app.client_id, user_account_id) |
| 109 | + return jsonable_encoder( |
| 110 | + { |
| 111 | + "code": code, |
| 112 | + } |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +class OAuthServerUserTokenApi(Resource): |
| 117 | + @setup_required |
| 118 | + @oauth_server_client_id_required |
| 119 | + def post(self, oauth_provider_app: OAuthProviderApp): |
| 120 | + parser = reqparse.RequestParser() |
| 121 | + parser.add_argument("grant_type", type=str, required=True, location="json") |
| 122 | + parser.add_argument("code", type=str, required=False, location="json") |
| 123 | + parser.add_argument("client_secret", type=str, required=False, location="json") |
| 124 | + parser.add_argument("redirect_uri", type=str, required=False, location="json") |
| 125 | + parser.add_argument("refresh_token", type=str, required=False, location="json") |
| 126 | + parsed_args = parser.parse_args() |
| 127 | + |
| 128 | + grant_type = OAuthGrantType(parsed_args["grant_type"]) |
| 129 | + |
| 130 | + if grant_type == OAuthGrantType.AUTHORIZATION_CODE: |
| 131 | + if not parsed_args["code"]: |
| 132 | + raise BadRequest("code is required") |
| 133 | + |
| 134 | + if parsed_args["client_secret"] != oauth_provider_app.client_secret: |
| 135 | + raise BadRequest("client_secret is invalid") |
| 136 | + |
| 137 | + if parsed_args["redirect_uri"] not in oauth_provider_app.redirect_uris: |
| 138 | + raise BadRequest("redirect_uri is invalid") |
| 139 | + |
| 140 | + access_token, refresh_token = OAuthServerService.sign_oauth_access_token( |
| 141 | + grant_type, code=parsed_args["code"], client_id=oauth_provider_app.client_id |
| 142 | + ) |
| 143 | + return jsonable_encoder( |
| 144 | + { |
| 145 | + "access_token": access_token, |
| 146 | + "token_type": "Bearer", |
| 147 | + "expires_in": OAUTH_ACCESS_TOKEN_EXPIRES_IN, |
| 148 | + "refresh_token": refresh_token, |
| 149 | + } |
| 150 | + ) |
| 151 | + elif grant_type == OAuthGrantType.REFRESH_TOKEN: |
| 152 | + if not parsed_args["refresh_token"]: |
| 153 | + raise BadRequest("refresh_token is required") |
| 154 | + |
| 155 | + access_token, refresh_token = OAuthServerService.sign_oauth_access_token( |
| 156 | + grant_type, refresh_token=parsed_args["refresh_token"], client_id=oauth_provider_app.client_id |
| 157 | + ) |
| 158 | + return jsonable_encoder( |
| 159 | + { |
| 160 | + "access_token": access_token, |
| 161 | + "token_type": "Bearer", |
| 162 | + "expires_in": OAUTH_ACCESS_TOKEN_EXPIRES_IN, |
| 163 | + "refresh_token": refresh_token, |
| 164 | + } |
| 165 | + ) |
| 166 | + else: |
| 167 | + raise BadRequest("invalid grant_type") |
| 168 | + |
| 169 | + |
| 170 | +class OAuthServerUserAccountApi(Resource): |
| 171 | + @setup_required |
| 172 | + @oauth_server_client_id_required |
| 173 | + @oauth_server_access_token_required |
| 174 | + def post(self, oauth_provider_app: OAuthProviderApp, account: Account): |
| 175 | + return jsonable_encoder( |
| 176 | + { |
| 177 | + "name": account.name, |
| 178 | + "email": account.email, |
| 179 | + "avatar": account.avatar, |
| 180 | + "interface_language": account.interface_language, |
| 181 | + "timezone": account.timezone, |
| 182 | + } |
| 183 | + ) |
| 184 | + |
| 185 | + |
| 186 | +api.add_resource(OAuthServerAppApi, "/oauth/provider") |
| 187 | +api.add_resource(OAuthServerUserAuthorizeApi, "/oauth/provider/authorize") |
| 188 | +api.add_resource(OAuthServerUserTokenApi, "/oauth/provider/token") |
| 189 | +api.add_resource(OAuthServerUserAccountApi, "/oauth/provider/account") |
0 commit comments