Skip to content

Commit

Permalink
Merge branch 'master' into feat/oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
bcbogdan committed Oct 25, 2024
2 parents 925c71a + d52a6c7 commit e2068e9
Show file tree
Hide file tree
Showing 215 changed files with 9,647 additions and 3,628 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ jobs:
command: wget https://go.dev/dl/go1.21.3.linux-amd64.tar.gz && rm -rf /usr/local/go && tar -C /usr/local -xzf go1.21.3.linux-amd64.tar.gz && export PATH=$PATH:/usr/local/go/bin
no_output_timeout: 30m
- run: apt-get update
- run: apt-get install -y python3.7-dev python3.7-venv || true
- run: apt-get install -y python3.8-dev python3.8-venv || true
- run: apt -y --fix-broken install
- run: pip3 install virtualenv
- run: cd v2/src/plugins/codeTypeChecking/pythonEnv/ && python3.7 -m virtualenv ./venv && source venv/bin/activate && pip install -r ./requirements.txt
- run: cd v2/src/plugins/codeTypeChecking/pythonEnv/ && python3.8 -m virtualenv ./venv && source venv/bin/activate && pip install -r ./requirements.txt
- run:
name: Setup Dart Env
command: cd v2/src/plugins/codeTypeChecking/dart_env && export PATH="$PATH:/root/flutter/bin" && flutter pub get
Expand Down
143 changes: 115 additions & 28 deletions v2/attackprotectionsuite/backend-setup.mdx

Large diffs are not rendered by default.

40 changes: 20 additions & 20 deletions v2/attackprotectionsuite/frontend-setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ Below is an example of how to implement request ID generation on your frontend:


```tsx
const PUBLIC_API_KEY = "<public-api-key>"; // Your public API key that you received from the SuperTokens team
const SDK_URL = "https://deviceid.supertokens.io/PqWNQ35Ydhm6WDUK/k9bwGCuvuA83Ad6s";
const PROXY_ENDPOINT_URL = "https://deviceid.supertokens.io/PqWNQ35Ydhm6WDUK/CnsdzKsyFKU8Q3h2"
const ENVIRONMENT_ID = "<environment-id>"; // Your environment ID that you received from the SuperTokens team
// Initialize the agent on page load.
const supertokensRequestIdPromise = import(SDK_URL + "?apiKey=" + PUBLIC_API_KEY).then((RequestId: any) => RequestId.load({
endpoint: [
PROXY_ENDPOINT_URL,
RequestId.defaultEndpoint
]
}));
// Initialize the agent on page load using your public API key that you received from the SuperTokens team.
const supertokensRequestIdPromise = require("https://deviceid.supertokens.io/PqWNQ35Ydhm6WDUK/k9bwGCuvuA83Ad6s?apiKey=<PUBLIC_API_KEY>")
.then((RequestId: any) => RequestId.load({
endpoint: [
'https://deviceid.supertokens.io/PqWNQ35Ydhm6WDUK/CnsdzKsyFKU8Q3h2',
RequestId.defaultEndpoint
]
}));

async function getRequestId() {
const sdk = await supertokensRequestIdPromise;
Expand All @@ -43,6 +41,10 @@ async function getRequestId() {
}
```

:::note
Make sure to replace the `<PUBLIC_API_KEY>` in the above string with the provided public API key.
:::

### Passing the Request ID to the Backend

Once you have generated the request ID on the frontend, you need to pass it to the backend. This is done by including the `requestId` property along with the value as part of the preAPIHook body from the initialisation of the recipes.
Expand All @@ -55,17 +57,15 @@ Below is a full example of how to configure the SDK and pass the request ID to t
```tsx
import EmailPassword from "supertokens-auth-react/recipe/emailpassword";

const PUBLIC_API_KEY = "<public-api-key>"; // Your public API key that you received from the SuperTokens team
const SDK_URL = "https://deviceid.supertokens.io/PqWNQ35Ydhm6WDUK/k9bwGCuvuA83Ad6s";
const PROXY_ENDPOINT_URL = "https://deviceid.supertokens.io/PqWNQ35Ydhm6WDUK/CnsdzKsyFKU8Q3h2"
const ENVIRONMENT_ID = "<environment-id>"; // Your environment ID that you received from the SuperTokens team
// Initialize the agent on page load.
const supertokensRequestIdPromise = import(SDK_URL + "?apiKey=" + PUBLIC_API_KEY).then((RequestId: any) => RequestId.load({
endpoint: [
PROXY_ENDPOINT_URL,
RequestId.defaultEndpoint
]
}));
// Initialize the agent on page load using your public API key that you received from the SuperTokens team.
const supertokensRequestIdPromise = require("https://deviceid.supertokens.io/PqWNQ35Ydhm6WDUK/k9bwGCuvuA83Ad6s?apiKey=<PUBLIC_API_KEY>")
.then((RequestId: any) => RequestId.load({
endpoint: [
'https://deviceid.supertokens.io/PqWNQ35Ydhm6WDUK/CnsdzKsyFKU8Q3h2',
RequestId.defaultEndpoint
]
}));

async function getRequestId() {
const sdk = await supertokensRequestIdPromise;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,30 +120,57 @@ func emailNotAllowed(email string) bool {

```python
from supertokens_python.recipe import emailpassword
from supertokens_python.recipe.emailpassword.interfaces import APIInterface, SignUpPostOkResult, SignUpPostEmailAlreadyExistsError, APIOptions
from supertokens_python.recipe.emailpassword.interfaces import (
APIInterface,
SignUpPostOkResult,
EmailAlreadyExistsError,
SignUpPostNotAllowedResponse,
APIOptions,
)
from supertokens_python.recipe.emailpassword.types import FormField
from typing import Any, Dict, Union, List
from supertokens_python.types import GeneralErrorResponse
from supertokens_python.recipe.session import SessionContainer


def override_apis(original_implementation: APIInterface):

# first we copy the original implementation
original_sign_up = original_implementation.sign_up_post

async def sign_up(form_fields: List[FormField], tenant_id: str,
api_options: APIOptions, user_context: Dict[str, Any]) -> Union[SignUpPostOkResult, SignUpPostEmailAlreadyExistsError, GeneralErrorResponse]:
async def sign_up(
form_fields: List[FormField],
tenant_id: str,
session: Union[SessionContainer, None],
should_try_linking_with_session_user: Union[bool, None],
api_options: APIOptions,
user_context: Dict[str, Any],
) -> Union[
SignUpPostOkResult,
EmailAlreadyExistsError,
SignUpPostNotAllowedResponse,
GeneralErrorResponse,
]:
email = ""
for i in range(len(form_fields)):
if form_fields[i].id == "email":
email = form_fields[i].value

if (is_not_allowed(email)):
if is_not_allowed(email):
# highlight-start
return GeneralErrorResponse(message="You are not allowed to sign up. Please contact the app's admin to get permission")
return GeneralErrorResponse(
message="You are not allowed to sign up. Please contact the app's admin to get permission"
)
# highlight-end

return await original_sign_up(form_fields, tenant_id, api_options, user_context)
return await original_sign_up(
form_fields,
tenant_id,
session,
should_try_linking_with_session_user,
api_options,
user_context,
)

original_implementation.sign_up_post = sign_up

Expand All @@ -155,11 +182,7 @@ def is_not_allowed(email: str):
return True


emailpassword.init(
override=emailpassword.InputOverrideConfig(
apis=override_apis
)
)
emailpassword.init(override=emailpassword.InputOverrideConfig(apis=override_apis))
```

</TabItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,23 @@ from supertokens_python.recipe.session.asyncio import get_all_session_handles_fo
from supertokens_python.recipe import session
from supertokens_python.recipe.session.interfaces import RecipeInterface
from typing import Any, Dict, Optional
from supertokens_python.types import RecipeUserId


def override_session_functions(original_implementation: RecipeInterface):

# first we copy the original implementation
original_create_new_session = original_implementation.create_new_session

async def create_new_session(user_id: str,
access_token_payload: Optional[Dict[str, Any]],
session_data_in_database: Optional[Dict[str, Any]],
disable_anti_csrf: Optional[bool],
tenant_id: str,
user_context: Dict[str, Any]):
async def create_new_session(
user_id: str,
recipe_user_id: RecipeUserId,
access_token_payload: Optional[Dict[str, Any]],
session_data_in_database: Optional[Dict[str, Any]],
disable_anti_csrf: Optional[bool],
tenant_id: str,
user_context: Dict[str, Any],
):
# highlight-start
existing_sessions = await get_all_session_handles_for_user(user_id)

Expand All @@ -128,19 +132,23 @@ def override_session_functions(original_implementation: RecipeInterface):
raise Exception("Session already exists on another device")

# no other session exists, and so we can continue with logging in this user
return await original_create_new_session(user_id, access_token_payload, session_data_in_database, disable_anti_csrf, tenant_id, user_context)
return await original_create_new_session(
user_id,
recipe_user_id,
access_token_payload,
session_data_in_database,
disable_anti_csrf,
tenant_id,
user_context,
)
# highlight-end

original_implementation.create_new_session = create_new_session

return original_implementation


session.init(
override=session.InputOverrideConfig(
functions=override_session_functions
)
)
session.init(override=session.InputOverrideConfig(functions=override_session_functions))
```

</TabItem>
Expand Down
37 changes: 26 additions & 11 deletions v2/emailpassword/advanced-customizations/apis-override/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -125,32 +125,47 @@ See all the [functions that can be overrided here](https://supertokens.com/docs/
```python
from supertokens_python import init, InputAppInfo
from supertokens_python.recipe import emailpassword
from supertokens_python.recipe.emailpassword.interfaces import APIInterface as EmailPasswordAPIInterface, APIOptions as EPAPIOptions
from supertokens_python.recipe.emailpassword.interfaces import (
APIInterface as EmailPasswordAPIInterface,
APIOptions,
)
from supertokens_python.recipe.emailpassword.types import FormField
from typing import List, Dict, Any
from typing import List, Dict, Any, Union
from supertokens_python.recipe.session import SessionContainer


# highlight-start
def override_email_password_apis(original_implementation: EmailPasswordAPIInterface):
original_sign_up_post = original_implementation.sign_up_post

async def sign_up_post(form_fields: List[FormField], tenant_id: str,
api_options: EPAPIOptions,
user_context: Dict[str, Any]):
async def sign_up_post(
form_fields: List[FormField],
tenant_id: str,
session: Union[SessionContainer, None],
should_try_linking_with_session_user: Union[bool, None],
api_options: APIOptions,
user_context: Dict[str, Any],
):
# TODO: custom logic

# or call the default behaviour as show below
return await original_sign_up_post(form_fields, tenant_id, api_options, user_context)
return await original_sign_up_post(
form_fields,
tenant_id,
session,
should_try_linking_with_session_user,
api_options,
user_context,
)

original_implementation.sign_up_post = sign_up_post
return original_implementation
# highlight-end


init(
app_info=InputAppInfo(
api_domain="...", app_name="...", website_domain="..."),

framework='...', # type: ignore
app_info=InputAppInfo(api_domain="...", app_name="...", website_domain="..."),
framework="...", # type: ignore
recipe_list=[
emailpassword.init(
# highlight-start
Expand All @@ -159,7 +174,7 @@ init(
)
# highlight-end
)
]
],
)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,27 +120,45 @@ See all the [functions that can be overrided here](https://supertokens.com/docs/
```python
from supertokens_python import init, InputAppInfo
from supertokens_python.recipe import emailpassword
from supertokens_python.recipe.emailpassword.interfaces import RecipeInterface as EPInterface
from typing import Dict, Any
from supertokens_python.recipe.emailpassword.interfaces import (
RecipeInterface as EPInterface,
)
from typing import Dict, Any, Union
from supertokens_python.recipe.session import SessionContainer


# highlight-start
def override_email_password_functions(original_implementation: EPInterface):
original_sign_up = original_implementation.sign_up

async def sign_up(email: str, password: str, tenant_id: str, user_context: Dict[str, Any]):
# TODO: custom logic
original_sign_up = original_implementation.sign_up

async def sign_up(
email: str,
password: str,
tenant_id: str,
session: Union[SessionContainer, None],
should_try_linking_with_session_user: Union[bool, None],
user_context: Dict[str, Any],
):
# TODO: custom logic

# or call the default behaviour as show below
return await original_sign_up(
email,
password,
tenant_id,
session,
should_try_linking_with_session_user,
user_context,
)

# or call the default behaviour as show below
return await original_sign_up(email, password, tenant_id, user_context)

original_implementation.sign_up = sign_up
return original_implementation
original_implementation.sign_up = sign_up
return original_implementation
# highlight-end


init(
app_info=InputAppInfo(api_domain="...", app_name="...", website_domain="..."),
framework='...', # type: ignore
framework="...", # type: ignore
recipe_list=[
emailpassword.init(
# highlight-start
Expand All @@ -149,7 +167,7 @@ init(
)
# highlight-end
)
]
],
)
```

Expand Down
Loading

0 comments on commit e2068e9

Please sign in to comment.