-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29a8969
commit 81cf53d
Showing
4 changed files
with
293 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from flask import Flask, request, jsonify | ||
from supertokens_python.recipe.thirdparty.types import ThirdPartyInfo | ||
from supertokens_python.types import AccountInfo | ||
from supertokens_python.syncio import ( | ||
get_user, | ||
delete_user, | ||
list_users_by_account_info, | ||
get_users_newest_first, | ||
get_users_oldest_first, | ||
) | ||
|
||
|
||
def add_supertokens_routes(app: Flask): | ||
@app.route("/test/supertokens/getuser", methods=["POST"]) # type: ignore | ||
def get_user_api(): # type: ignore | ||
assert request.json is not None | ||
response = get_user(request.json["userId"], request.json.get("userContext")) | ||
return jsonify(None if response is None else response.to_json()) | ||
|
||
@app.route("/test/supertokens/deleteuser", methods=["POST"]) # type: ignore | ||
def delete_user_api(): # type: ignore | ||
assert request.json is not None | ||
delete_user( | ||
request.json["userId"], | ||
request.json["removeAllLinkedAccounts"], | ||
request.json.get("userContext"), | ||
) | ||
return jsonify({"status": "OK"}) | ||
|
||
@app.route("/test/supertokens/listusersbyaccountinfo", methods=["POST"]) # type: ignore | ||
def list_users_by_account_info_api(): # type: ignore | ||
assert request.json is not None | ||
response = list_users_by_account_info( | ||
request.json["tenantId"], | ||
AccountInfo( | ||
email=request.json["accountInfo"].get("email", None), | ||
phone_number=request.json["accountInfo"].get("phoneNumber", None), | ||
third_party=( | ||
None | ||
if "thirdParty" not in request.json["accountInfo"] | ||
else ThirdPartyInfo( | ||
third_party_id=request.json["accountInfo"]["thirdParty"][ | ||
"thirdPartyId" | ||
], | ||
third_party_user_id=request.json["accountInfo"]["thirdParty"][ | ||
"id" | ||
], | ||
) | ||
), | ||
), | ||
request.json["doUnionOfAccountInfo"], | ||
request.json.get("userContext"), | ||
) | ||
|
||
return jsonify([r.to_json() for r in response]) | ||
|
||
@app.route("/test/supertokens/getusersnewestfirst", methods=["POST"]) # type: ignore | ||
def get_users_newest_first_api(): # type: ignore | ||
assert request.json is not None | ||
response = get_users_newest_first( | ||
include_recipe_ids=request.json["includeRecipeIds"], | ||
limit=request.json["limit"], | ||
pagination_token=request.json["paginationToken"], | ||
tenant_id=request.json["tenantId"], | ||
user_context=request.json.get("userContext"), | ||
) | ||
return jsonify( | ||
{ | ||
"nextPaginationToken": response.next_pagination_token, | ||
"users": [r.to_json() for r in response.users], | ||
} | ||
) | ||
|
||
@app.route("/test/supertokens/getusersoldestfirst", methods=["POST"]) # type: ignore | ||
def get_users_oldest_first_api(): # type: ignore | ||
assert request.json is not None | ||
response = get_users_oldest_first( | ||
include_recipe_ids=request.json["includeRecipeIds"], | ||
limit=request.json["limit"], | ||
pagination_token=request.json["paginationToken"], | ||
tenant_id=request.json["tenantId"], | ||
user_context=request.json.get("userContext"), | ||
) | ||
return jsonify( | ||
{ | ||
"nextPaginationToken": response.next_pagination_token, | ||
"users": [r.to_json() for r in response.users], | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,11 @@ | |
ShouldAutomaticallyLink, | ||
ShouldNotAutomaticallyLink, | ||
) | ||
from supertokens_python.recipe.thirdparty.types import ( | ||
RawUserInfoFromProvider, | ||
UserInfo, | ||
UserInfoEmail, | ||
) | ||
from supertokens_python.types import AccountInfo, RecipeUserId | ||
from supertokens_python.types import APIResponse, User | ||
|
||
|
@@ -124,7 +129,164 @@ async def func( | |
|
||
return func | ||
|
||
raise Exception("Unknown eval string") | ||
if eval_str.startswith("thirdparty.init.signInAndUpFeature.providers"): | ||
|
||
def custom_provider(provider: Any): | ||
if "custom-ev" in eval_str: | ||
|
||
def exchange_auth_code_for_oauth_tokens1( | ||
redirect_uri_info: Any, # pylint: disable=unused-argument | ||
user_context: Any, # pylint: disable=unused-argument | ||
) -> Any: | ||
return {} | ||
|
||
def get_user_info1( | ||
oauth_tokens: Any, | ||
user_context: Any, # pylint: disable=unused-argument | ||
): # pylint: disable=unused-argument | ||
return UserInfo( | ||
third_party_user_id=oauth_tokens.get("userId", "user"), | ||
email=UserInfoEmail( | ||
email=oauth_tokens.get("email", "[email protected]"), | ||
is_verified=True, | ||
), | ||
raw_user_info_from_provider=RawUserInfoFromProvider( | ||
from_id_token_payload=None, | ||
from_user_info_api=None, | ||
), | ||
) | ||
|
||
provider.exchange_auth_code_for_oauth_tokens = ( | ||
exchange_auth_code_for_oauth_tokens1 | ||
) | ||
provider.get_user_info = get_user_info1 | ||
return provider | ||
|
||
if "custom-no-ev" in eval_str: | ||
|
||
def exchange_auth_code_for_oauth_tokens2( | ||
redirect_uri_info: Any, # pylint: disable=unused-argument | ||
user_context: Any, # pylint: disable=unused-argument | ||
) -> Any: | ||
return {} | ||
|
||
def get_user_info2( | ||
oauth_tokens: Any, user_context: Any | ||
): # pylint: disable=unused-argument | ||
return UserInfo( | ||
third_party_user_id=oauth_tokens.get("userId", "user"), | ||
email=UserInfoEmail( | ||
email=oauth_tokens.get("email", "[email protected]"), | ||
is_verified=False, | ||
), | ||
raw_user_info_from_provider=RawUserInfoFromProvider( | ||
from_id_token_payload=None, | ||
from_user_info_api=None, | ||
), | ||
) | ||
|
||
provider.exchange_auth_code_for_oauth_tokens = ( | ||
exchange_auth_code_for_oauth_tokens2 | ||
) | ||
provider.get_user_info = get_user_info2 | ||
return provider | ||
|
||
if "custom2" in eval_str: | ||
|
||
def exchange_auth_code_for_oauth_tokens3( | ||
redirect_uri_info: Any, | ||
user_context: Any, # pylint: disable=unused-argument | ||
) -> Any: | ||
return redirect_uri_info["redirectURIQueryParams"] | ||
|
||
def get_user_info3( | ||
oauth_tokens: Any, user_context: Any | ||
): # pylint: disable=unused-argument | ||
return UserInfo( | ||
third_party_user_id=f"custom2{oauth_tokens['email']}", | ||
email=UserInfoEmail( | ||
email=oauth_tokens["email"], | ||
is_verified=True, | ||
), | ||
raw_user_info_from_provider=RawUserInfoFromProvider( | ||
from_id_token_payload=None, | ||
from_user_info_api=None, | ||
), | ||
) | ||
|
||
provider.exchange_auth_code_for_oauth_tokens = ( | ||
exchange_auth_code_for_oauth_tokens3 | ||
) | ||
provider.get_user_info = get_user_info3 | ||
return provider | ||
|
||
if "custom3" in eval_str: | ||
|
||
def exchange_auth_code_for_oauth_tokens4( | ||
redirect_uri_info: Any, | ||
user_context: Any, # pylint: disable=unused-argument | ||
) -> Any: | ||
return redirect_uri_info["redirectURIQueryParams"] | ||
|
||
def get_user_info4( | ||
oauth_tokens: Any, user_context: Any | ||
): # pylint: disable=unused-argument | ||
return UserInfo( | ||
third_party_user_id=oauth_tokens["email"], | ||
email=UserInfoEmail( | ||
email=oauth_tokens["email"], | ||
is_verified=True, | ||
), | ||
raw_user_info_from_provider=RawUserInfoFromProvider( | ||
from_id_token_payload=None, | ||
from_user_info_api=None, | ||
), | ||
) | ||
|
||
provider.exchange_auth_code_for_oauth_tokens = ( | ||
exchange_auth_code_for_oauth_tokens4 | ||
) | ||
provider.get_user_info = get_user_info4 | ||
return provider | ||
|
||
if "custom" in eval_str: | ||
|
||
def exchange_auth_code_for_oauth_tokens5( | ||
redirect_uri_info: Any, | ||
user_context: Any, # pylint: disable=unused-argument | ||
) -> Any: | ||
return redirect_uri_info | ||
|
||
def get_user_info5( | ||
oauth_tokens: Any, user_context: Any | ||
): # pylint: disable=unused-argument | ||
if oauth_tokens.get("error"): | ||
raise Exception("Credentials error") | ||
return UserInfo( | ||
third_party_user_id=oauth_tokens.get("userId", "userId"), | ||
email=( | ||
None | ||
if oauth_tokens.get("email") is None | ||
else UserInfoEmail( | ||
email=oauth_tokens.get("email"), | ||
is_verified=oauth_tokens.get("isVerified", False), | ||
) | ||
), | ||
raw_user_info_from_provider=RawUserInfoFromProvider( | ||
from_id_token_payload=None, | ||
from_user_info_api=None, | ||
), | ||
) | ||
|
||
provider.exchange_auth_code_for_oauth_tokens = ( | ||
exchange_auth_code_for_oauth_tokens5 | ||
) | ||
provider.get_user_info = get_user_info5 | ||
return provider | ||
|
||
return custom_provider | ||
|
||
raise Exception("Unknown eval string: " + eval_str) | ||
|
||
|
||
class OverrideParams(APIResponse): | ||
|