coreason-identity 0.4.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- coreason_identity-0.4.1/LICENSE +57 -0
- coreason_identity-0.4.1/NOTICE +8 -0
- coreason_identity-0.4.1/PKG-INFO +107 -0
- coreason_identity-0.4.1/README.md +77 -0
- coreason_identity-0.4.1/pyproject.toml +74 -0
- coreason_identity-0.4.1/src/coreason_identity/__init__.py +44 -0
- coreason_identity-0.4.1/src/coreason_identity/config.py +59 -0
- coreason_identity-0.4.1/src/coreason_identity/device_flow_client.py +213 -0
- coreason_identity-0.4.1/src/coreason_identity/exceptions.py +52 -0
- coreason_identity-0.4.1/src/coreason_identity/identity_mapper.py +175 -0
- coreason_identity-0.4.1/src/coreason_identity/main.py +94 -0
- coreason_identity-0.4.1/src/coreason_identity/manager.py +176 -0
- coreason_identity-0.4.1/src/coreason_identity/models.py +101 -0
- coreason_identity-0.4.1/src/coreason_identity/oidc_provider.py +127 -0
- coreason_identity-0.4.1/src/coreason_identity/utils/__init__.py +13 -0
- coreason_identity-0.4.1/src/coreason_identity/utils/logger.py +46 -0
- coreason_identity-0.4.1/src/coreason_identity/validator.py +169 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# The Prosperity Public License 3.0.0
|
|
2
|
+
|
|
3
|
+
Contributor: CoReason, Inc.
|
|
4
|
+
|
|
5
|
+
Source Code: https://github.com/CoReason-AI/coreason_identity
|
|
6
|
+
|
|
7
|
+
## Purpose
|
|
8
|
+
|
|
9
|
+
This license allows you to use and share this software for noncommercial purposes for free and to try this software for commercial purposes for thirty days.
|
|
10
|
+
|
|
11
|
+
## Agreement
|
|
12
|
+
|
|
13
|
+
In order to receive this license, you have to agree to its rules. Those rules are both obligations under that agreement and conditions to your license. Don't do anything with this software that triggers a rule you can't or won't follow.
|
|
14
|
+
|
|
15
|
+
## Notices
|
|
16
|
+
|
|
17
|
+
Make sure everyone who gets a copy of any part of this software from you, with or without changes, also gets the text of this license and the contributor and source code lines above.
|
|
18
|
+
|
|
19
|
+
## Commercial Trial
|
|
20
|
+
|
|
21
|
+
Limit your use of this software for commercial purposes to a thirty-day trial period. If you use this software for work, your company gets one trial period for all personnel, not one trial per person.
|
|
22
|
+
|
|
23
|
+
## Contributions Back
|
|
24
|
+
|
|
25
|
+
Developing feedback, changes, or additions that you contribute back to the contributor on the terms of a standardized public software license such as [the Blue Oak Model License 1.0.0](https://blueoakcouncil.org/license/1.0.0), [the Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html), [the MIT license](https://spdx.org/licenses/MIT.html), or [the two-clause BSD license](https://spdx.org/licenses/BSD-2-Clause.html) doesn't count as use for a commercial purpose.
|
|
26
|
+
|
|
27
|
+
## Personal Uses
|
|
28
|
+
|
|
29
|
+
Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, doesn't count as use for a commercial purpose.
|
|
30
|
+
|
|
31
|
+
## Noncommercial Organizations
|
|
32
|
+
|
|
33
|
+
Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution doesn't count as use for a commercial purpose regardless of the source of funding or obligations resulting from the funding.
|
|
34
|
+
|
|
35
|
+
## Defense
|
|
36
|
+
|
|
37
|
+
Don't make any legal claim against anyone accusing this software, with or without changes, alone or with other technology, of infringing any patent.
|
|
38
|
+
|
|
39
|
+
## Copyright
|
|
40
|
+
|
|
41
|
+
The contributor licenses you to do everything with this software that would otherwise infringe their copyright in it.
|
|
42
|
+
|
|
43
|
+
## Patent
|
|
44
|
+
|
|
45
|
+
The contributor licenses you to do everything with this software that would otherwise infringe any patents they can license or become able to license.
|
|
46
|
+
|
|
47
|
+
## Reliability
|
|
48
|
+
|
|
49
|
+
The contributor can't revoke this license.
|
|
50
|
+
|
|
51
|
+
## Excuse
|
|
52
|
+
|
|
53
|
+
You're excused for unknowingly breaking [Notices](#notices) if you take all practical steps to comply within thirty days of learning you broke the rule.
|
|
54
|
+
|
|
55
|
+
## No Liability
|
|
56
|
+
|
|
57
|
+
***As far as the law allows, this software comes as is, without any warranty or condition, and the contributor won't be liable to anyone for any damages related to this software or this license, under any kind of legal claim.***
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Copyright (c) 2025 CoReason, Inc.. All Rights Reserved
|
|
2
|
+
|
|
3
|
+
This software is licensed under the Prosperity Public License 3.0.0.
|
|
4
|
+
The issuer of the Prosperity Public License for this software is CoReason, Inc..
|
|
5
|
+
|
|
6
|
+
For a commercial version of this software, please contact us at gowtham.rao@coreason.ai.
|
|
7
|
+
|
|
8
|
+
GENESIS COMMIT: Initializing repository coreason_identity per CoReason Clean Room Protocol PIP-001. This repository is established as an independently created De Novo development environment, commencing on 2026-01-01. I, Gowtham A Rao certify that this date is subsequent to my individual Temporal Firewall Date.
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: coreason_identity
|
|
3
|
+
Version: 0.4.1
|
|
4
|
+
Summary: Decoupled authentication middleware, abstracting OIDC and OAuth2 protocols from the main application.
|
|
5
|
+
License: Prosperity-3.0
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
License-File: NOTICE
|
|
8
|
+
Author: Gowtham A Rao
|
|
9
|
+
Author-email: gowtham.rao@coreason.ai
|
|
10
|
+
Requires-Python: >=3.11, <3.15
|
|
11
|
+
Classifier: License :: Other/Proprietary License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Requires-Dist: aiofiles (>=23.0.0,<24.0.0)
|
|
16
|
+
Requires-Dist: anyio (>=4.12.1,<5.0.0)
|
|
17
|
+
Requires-Dist: authlib (>=1.6.6,<2.0.0)
|
|
18
|
+
Requires-Dist: email-validator (>=2.3.0,<3.0.0)
|
|
19
|
+
Requires-Dist: httpx (>=0.28.1,<0.29.0)
|
|
20
|
+
Requires-Dist: loguru (>=0.7.2,<0.8.0)
|
|
21
|
+
Requires-Dist: opentelemetry-api (>=1.39.1,<2.0.0)
|
|
22
|
+
Requires-Dist: pydantic (>=2.12.5,<3.0.0)
|
|
23
|
+
Requires-Dist: pydantic-settings (>=2.12.0,<3.0.0)
|
|
24
|
+
Requires-Dist: types-aiofiles (>=23.0.0,<24.0.0)
|
|
25
|
+
Project-URL: Documentation, https://github.com/CoReason-AI/coreason_identity
|
|
26
|
+
Project-URL: Homepage, https://github.com/CoReason-AI/coreason_identity
|
|
27
|
+
Project-URL: Repository, https://github.com/CoReason-AI/coreason_identity
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# coreason-identity
|
|
31
|
+
|
|
32
|
+
Decoupled authentication middleware, abstracting OIDC and OAuth2 protocols from the main application.
|
|
33
|
+
|
|
34
|
+
[](https://github.com/CoReason-AI)
|
|
35
|
+
[](https://img.shields.io/badge/license-Prosperity%203.0-blue)
|
|
36
|
+
[](https://github.com/CoReason-AI/coreason_identity/actions)
|
|
37
|
+
[](https://github.com/astral-sh/ruff)
|
|
38
|
+
[](docs/product_requirements.md)
|
|
39
|
+
|
|
40
|
+
## Overview
|
|
41
|
+
|
|
42
|
+
`coreason-identity` ("The Bouncer") handles all Authentication (AuthN) and Role-Based Access Control (AuthZ) for the CoReason platform. It enforces a strict "Bouncer" philosophy: it checks IDs and checks lists but does not issue IDs.
|
|
43
|
+
|
|
44
|
+
The package standardizes:
|
|
45
|
+
* **Protocol:** OIDC (OpenID Connect).
|
|
46
|
+
* **Identity Provider:** Auth0 or Keycloak.
|
|
47
|
+
* **Library:** Authlib.
|
|
48
|
+
|
|
49
|
+
## Features
|
|
50
|
+
|
|
51
|
+
Based on the [Product Requirements](docs/product_requirements.md):
|
|
52
|
+
|
|
53
|
+
* **OIDCProvider:** Fetches and caches JWKS from the OIDC Discovery URL (LRU Cache).
|
|
54
|
+
* **TokenValidator:** Validates JWT signatures, standard claims (`exp`, `iss`, `aud`), and enforces strict audience checks to prevent "Confused Deputy" attacks.
|
|
55
|
+
* **IdentityMapper:** Maps IdP claims to a standardized `UserContext` model, handling project context extraction and group-to-permission mapping.
|
|
56
|
+
* **DeviceFlowClient:** Implements RFC 8628 OAuth 2.0 Device Authorization Grant for headless CLI authentication.
|
|
57
|
+
* **Observability:** Emits OpenTelemetry spans and secure logs (PII hashed).
|
|
58
|
+
|
|
59
|
+
## Installation
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install coreason-identity
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Usage
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from coreason_identity import IdentityManager, CoreasonIdentityConfig, InvalidTokenError
|
|
69
|
+
|
|
70
|
+
# 1. Initialize (The Borrowing)
|
|
71
|
+
config = CoreasonIdentityConfig(domain="auth.coreason.com", audience="api://coreason")
|
|
72
|
+
identity = IdentityManager(config)
|
|
73
|
+
|
|
74
|
+
# 2. Validate (The Bouncer)
|
|
75
|
+
try:
|
|
76
|
+
# Validate a raw Bearer token
|
|
77
|
+
user_context = identity.validate_token(auth_header="Bearer eyJ...")
|
|
78
|
+
|
|
79
|
+
# Access canonical Identity Passport fields
|
|
80
|
+
print(f"User {user_context.user_id} ({user_context.email}) is active.")
|
|
81
|
+
|
|
82
|
+
# Check groups for Row-Level Security
|
|
83
|
+
if "admin" in user_context.groups:
|
|
84
|
+
print("Admin access granted.")
|
|
85
|
+
|
|
86
|
+
# Access extended attributes
|
|
87
|
+
project = user_context.claims.get("project_context")
|
|
88
|
+
print(f"Authorized for project: {project}")
|
|
89
|
+
|
|
90
|
+
except InvalidTokenError:
|
|
91
|
+
# Handle invalid tokens (expired, bad signature, wrong audience, etc.)
|
|
92
|
+
print("Access denied.")
|
|
93
|
+
|
|
94
|
+
# 3. CLI Login (The Device Flow)
|
|
95
|
+
# Initiate the flow
|
|
96
|
+
flow = identity.start_device_login()
|
|
97
|
+
print(f"Go to {flow.verification_uri} and enter {flow.user_code}")
|
|
98
|
+
|
|
99
|
+
# Poll for tokens
|
|
100
|
+
try:
|
|
101
|
+
tokens = identity.await_device_token(flow)
|
|
102
|
+
print("Login successful!")
|
|
103
|
+
print(f"Access Token: {tokens['access_token']}")
|
|
104
|
+
except Exception as e:
|
|
105
|
+
print(f"Login failed: {e}")
|
|
106
|
+
```
|
|
107
|
+
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# coreason-identity
|
|
2
|
+
|
|
3
|
+
Decoupled authentication middleware, abstracting OIDC and OAuth2 protocols from the main application.
|
|
4
|
+
|
|
5
|
+
[](https://github.com/CoReason-AI)
|
|
6
|
+
[](https://img.shields.io/badge/license-Prosperity%203.0-blue)
|
|
7
|
+
[](https://github.com/CoReason-AI/coreason_identity/actions)
|
|
8
|
+
[](https://github.com/astral-sh/ruff)
|
|
9
|
+
[](docs/product_requirements.md)
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
`coreason-identity` ("The Bouncer") handles all Authentication (AuthN) and Role-Based Access Control (AuthZ) for the CoReason platform. It enforces a strict "Bouncer" philosophy: it checks IDs and checks lists but does not issue IDs.
|
|
14
|
+
|
|
15
|
+
The package standardizes:
|
|
16
|
+
* **Protocol:** OIDC (OpenID Connect).
|
|
17
|
+
* **Identity Provider:** Auth0 or Keycloak.
|
|
18
|
+
* **Library:** Authlib.
|
|
19
|
+
|
|
20
|
+
## Features
|
|
21
|
+
|
|
22
|
+
Based on the [Product Requirements](docs/product_requirements.md):
|
|
23
|
+
|
|
24
|
+
* **OIDCProvider:** Fetches and caches JWKS from the OIDC Discovery URL (LRU Cache).
|
|
25
|
+
* **TokenValidator:** Validates JWT signatures, standard claims (`exp`, `iss`, `aud`), and enforces strict audience checks to prevent "Confused Deputy" attacks.
|
|
26
|
+
* **IdentityMapper:** Maps IdP claims to a standardized `UserContext` model, handling project context extraction and group-to-permission mapping.
|
|
27
|
+
* **DeviceFlowClient:** Implements RFC 8628 OAuth 2.0 Device Authorization Grant for headless CLI authentication.
|
|
28
|
+
* **Observability:** Emits OpenTelemetry spans and secure logs (PII hashed).
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install coreason-identity
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from coreason_identity import IdentityManager, CoreasonIdentityConfig, InvalidTokenError
|
|
40
|
+
|
|
41
|
+
# 1. Initialize (The Borrowing)
|
|
42
|
+
config = CoreasonIdentityConfig(domain="auth.coreason.com", audience="api://coreason")
|
|
43
|
+
identity = IdentityManager(config)
|
|
44
|
+
|
|
45
|
+
# 2. Validate (The Bouncer)
|
|
46
|
+
try:
|
|
47
|
+
# Validate a raw Bearer token
|
|
48
|
+
user_context = identity.validate_token(auth_header="Bearer eyJ...")
|
|
49
|
+
|
|
50
|
+
# Access canonical Identity Passport fields
|
|
51
|
+
print(f"User {user_context.user_id} ({user_context.email}) is active.")
|
|
52
|
+
|
|
53
|
+
# Check groups for Row-Level Security
|
|
54
|
+
if "admin" in user_context.groups:
|
|
55
|
+
print("Admin access granted.")
|
|
56
|
+
|
|
57
|
+
# Access extended attributes
|
|
58
|
+
project = user_context.claims.get("project_context")
|
|
59
|
+
print(f"Authorized for project: {project}")
|
|
60
|
+
|
|
61
|
+
except InvalidTokenError:
|
|
62
|
+
# Handle invalid tokens (expired, bad signature, wrong audience, etc.)
|
|
63
|
+
print("Access denied.")
|
|
64
|
+
|
|
65
|
+
# 3. CLI Login (The Device Flow)
|
|
66
|
+
# Initiate the flow
|
|
67
|
+
flow = identity.start_device_login()
|
|
68
|
+
print(f"Go to {flow.verification_uri} and enter {flow.user_code}")
|
|
69
|
+
|
|
70
|
+
# Poll for tokens
|
|
71
|
+
try:
|
|
72
|
+
tokens = identity.await_device_token(flow)
|
|
73
|
+
print("Login successful!")
|
|
74
|
+
print(f"Access Token: {tokens['access_token']}")
|
|
75
|
+
except Exception as e:
|
|
76
|
+
print(f"Login failed: {e}")
|
|
77
|
+
```
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "coreason_identity"
|
|
3
|
+
version = "0.4.1"
|
|
4
|
+
description = "Decoupled authentication middleware, abstracting OIDC and OAuth2 protocols from the main application."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11, <3.15"
|
|
7
|
+
authors = [
|
|
8
|
+
{ name = "Gowtham A Rao", email = "gowtham.rao@coreason.ai" },
|
|
9
|
+
]
|
|
10
|
+
license = {text = "Prosperity-3.0"}
|
|
11
|
+
classifiers = [
|
|
12
|
+
"License :: Other/Proprietary License",
|
|
13
|
+
"Programming Language :: Python :: 3.11",
|
|
14
|
+
"Programming Language :: Python :: 3.12",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
]
|
|
17
|
+
dependencies = [
|
|
18
|
+
"loguru>=0.7.2,<0.8.0",
|
|
19
|
+
"authlib>=1.6.6,<2.0.0",
|
|
20
|
+
"pydantic>=2.12.5,<3.0.0",
|
|
21
|
+
"pydantic-settings>=2.12.0,<3.0.0",
|
|
22
|
+
"httpx>=0.28.1,<0.29.0",
|
|
23
|
+
"email-validator>=2.3.0,<3.0.0",
|
|
24
|
+
"opentelemetry-api>=1.39.1,<2.0.0",
|
|
25
|
+
"anyio>=4.12.1,<5.0.0",
|
|
26
|
+
"aiofiles>=23.0.0,<24.0.0",
|
|
27
|
+
"types-aiofiles>=23.0.0,<24.0.0",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/CoReason-AI/coreason_identity"
|
|
32
|
+
Repository = "https://github.com/CoReason-AI/coreason_identity"
|
|
33
|
+
Documentation = "https://github.com/CoReason-AI/coreason_identity"
|
|
34
|
+
|
|
35
|
+
[tool.poetry]
|
|
36
|
+
packages = [{include = "coreason_identity", from = "src"}]
|
|
37
|
+
include = [{ path = "NOTICE", format = ["sdist", "wheel"] }]
|
|
38
|
+
|
|
39
|
+
[tool.poetry.group.dev.dependencies]
|
|
40
|
+
pytest = "^8.2.2"
|
|
41
|
+
ruff = "^0.14.14"
|
|
42
|
+
pre-commit = "^3.7.1"
|
|
43
|
+
pytest-cov = "^5.0.0"
|
|
44
|
+
mkdocs = "^1.6.0"
|
|
45
|
+
mkdocs-material = "^9.5.26"
|
|
46
|
+
opentelemetry-sdk = "^1.39.1"
|
|
47
|
+
mypy = "^1.19.1"
|
|
48
|
+
pytest-asyncio = "^1.3.0"
|
|
49
|
+
|
|
50
|
+
[build-system]
|
|
51
|
+
requires = ["poetry-core"]
|
|
52
|
+
build-backend = "poetry.core.masonry.api"
|
|
53
|
+
|
|
54
|
+
[tool.ruff]
|
|
55
|
+
line-length = 120
|
|
56
|
+
target-version = "py311"
|
|
57
|
+
|
|
58
|
+
[tool.ruff.lint]
|
|
59
|
+
select = ["E", "F", "B", "I"]
|
|
60
|
+
ignore = []
|
|
61
|
+
|
|
62
|
+
[tool.mypy]
|
|
63
|
+
python_version = "3.11"
|
|
64
|
+
strict = true
|
|
65
|
+
ignore_missing_imports = true
|
|
66
|
+
plugins = ["pydantic.mypy"]
|
|
67
|
+
|
|
68
|
+
[tool.pytest.ini_options]
|
|
69
|
+
addopts = "--cov=src --cov-report=term-missing --cov-fail-under=100"
|
|
70
|
+
testpaths = ["tests"]
|
|
71
|
+
asyncio_mode = "auto"
|
|
72
|
+
|
|
73
|
+
[tool.coverage.run]
|
|
74
|
+
omit = ["tests/*"]
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Copyright (c) 2025 CoReason, Inc.
|
|
2
|
+
#
|
|
3
|
+
# This software is proprietary and dual-licensed.
|
|
4
|
+
# Licensed under the Prosperity Public License 3.0 (the "License").
|
|
5
|
+
# A copy of the license is available at https://prosperitylicense.com/versions/3.0.0
|
|
6
|
+
# For details, see the LICENSE file.
|
|
7
|
+
# Commercial use beyond a 30-day trial requires a separate license.
|
|
8
|
+
#
|
|
9
|
+
# Source Code: https://github.com/CoReason-AI/coreason_identity
|
|
10
|
+
|
|
11
|
+
"""
|
|
12
|
+
Coreason Identity SDK
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from coreason_identity.config import CoreasonIdentityConfig
|
|
16
|
+
from coreason_identity.exceptions import (
|
|
17
|
+
CoreasonIdentityError,
|
|
18
|
+
InsufficientPermissionsError,
|
|
19
|
+
InvalidAudienceError,
|
|
20
|
+
InvalidTokenError,
|
|
21
|
+
SignatureVerificationError,
|
|
22
|
+
TokenExpiredError,
|
|
23
|
+
)
|
|
24
|
+
from coreason_identity.manager import IdentityManager, IdentityManagerAsync
|
|
25
|
+
from coreason_identity.models import DeviceFlowResponse, TokenResponse, UserContext
|
|
26
|
+
|
|
27
|
+
__version__ = "0.3.0"
|
|
28
|
+
__author__ = "Gowtham A Rao"
|
|
29
|
+
__email__ = "gowtham.rao@coreason.ai"
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"CoreasonIdentityConfig",
|
|
33
|
+
"IdentityManager",
|
|
34
|
+
"IdentityManagerAsync",
|
|
35
|
+
"UserContext",
|
|
36
|
+
"TokenResponse",
|
|
37
|
+
"DeviceFlowResponse",
|
|
38
|
+
"CoreasonIdentityError",
|
|
39
|
+
"InvalidTokenError",
|
|
40
|
+
"InvalidAudienceError",
|
|
41
|
+
"SignatureVerificationError",
|
|
42
|
+
"TokenExpiredError",
|
|
43
|
+
"InsufficientPermissionsError",
|
|
44
|
+
]
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Copyright (c) 2025 CoReason, Inc.
|
|
2
|
+
#
|
|
3
|
+
# This software is proprietary and dual-licensed.
|
|
4
|
+
# Licensed under the Prosperity Public License 3.0 (the "License").
|
|
5
|
+
# A copy of the license is available at https://prosperitylicense.com/versions/3.0.0
|
|
6
|
+
# For details, see the LICENSE file.
|
|
7
|
+
# Commercial use beyond a 30-day trial requires a separate license.
|
|
8
|
+
#
|
|
9
|
+
# Source Code: https://github.com/CoReason-AI/coreason_identity
|
|
10
|
+
|
|
11
|
+
"""
|
|
12
|
+
Configuration for the coreason-identity package.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from typing import Optional
|
|
16
|
+
from urllib.parse import urlparse
|
|
17
|
+
|
|
18
|
+
from pydantic import field_validator
|
|
19
|
+
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class CoreasonIdentityConfig(BaseSettings):
|
|
23
|
+
"""
|
|
24
|
+
Configuration settings for coreason-identity.
|
|
25
|
+
|
|
26
|
+
Attributes:
|
|
27
|
+
domain (str): The domain of the Identity Provider (e.g. auth.coreason.com).
|
|
28
|
+
audience (str): The expected audience for the token.
|
|
29
|
+
client_id (Optional[str]): The OIDC Client ID (required for device flow).
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
model_config = SettingsConfigDict(
|
|
33
|
+
env_prefix="COREASON_AUTH_",
|
|
34
|
+
case_sensitive=False,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
domain: str
|
|
38
|
+
audience: str
|
|
39
|
+
client_id: Optional[str] = None
|
|
40
|
+
|
|
41
|
+
@field_validator("domain")
|
|
42
|
+
@classmethod
|
|
43
|
+
def normalize_domain(cls, v: str) -> str:
|
|
44
|
+
"""
|
|
45
|
+
Ensures domain is just the hostname (e.g. auth.coreason.com).
|
|
46
|
+
Strips scheme and path if present.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
v: The domain string to normalize.
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
The normalized hostname string.
|
|
53
|
+
"""
|
|
54
|
+
v = v.strip().lower()
|
|
55
|
+
if "://" not in v:
|
|
56
|
+
v = f"https://{v}"
|
|
57
|
+
|
|
58
|
+
parsed = urlparse(v)
|
|
59
|
+
return parsed.netloc or v
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# Copyright (c) 2025 CoReason, Inc.
|
|
2
|
+
#
|
|
3
|
+
# This software is proprietary and dual-licensed.
|
|
4
|
+
# Licensed under the Prosperity Public License 3.0 (the "License").
|
|
5
|
+
# A copy of the license is available at https://prosperitylicense.com/versions/3.0.0
|
|
6
|
+
# For details, see the LICENSE file.
|
|
7
|
+
# Commercial use beyond a 30-day trial requires a separate license.
|
|
8
|
+
#
|
|
9
|
+
# Source Code: https://github.com/CoReason-AI/coreason_identity
|
|
10
|
+
|
|
11
|
+
"""
|
|
12
|
+
DeviceFlowClient component for handling OAuth 2.0 Device Authorization Grant.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import time
|
|
16
|
+
from typing import Dict, Optional
|
|
17
|
+
from urllib.parse import urljoin
|
|
18
|
+
|
|
19
|
+
import anyio
|
|
20
|
+
import httpx
|
|
21
|
+
from pydantic import ValidationError
|
|
22
|
+
|
|
23
|
+
from coreason_identity.exceptions import CoreasonIdentityError
|
|
24
|
+
from coreason_identity.models import DeviceFlowResponse, TokenResponse
|
|
25
|
+
from coreason_identity.utils.logger import logger
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class DeviceFlowClient:
|
|
29
|
+
"""
|
|
30
|
+
Handles the OAuth 2.0 Device Authorization Grant flow (RFC 8628).
|
|
31
|
+
|
|
32
|
+
Attributes:
|
|
33
|
+
client_id (str): The OIDC Client ID.
|
|
34
|
+
idp_url (str): The base URL of the Identity Provider.
|
|
35
|
+
scope (str): The scopes to request.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(
|
|
39
|
+
self, client_id: str, idp_url: str, client: httpx.AsyncClient, scope: str = "openid profile email"
|
|
40
|
+
) -> None:
|
|
41
|
+
"""
|
|
42
|
+
Initialize the DeviceFlowClient.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
client_id: The OIDC Client ID.
|
|
46
|
+
idp_url: The base URL of the Identity Provider (e.g., https://my-tenant.auth0.com).
|
|
47
|
+
client: The async HTTP client to use for requests.
|
|
48
|
+
scope: The scopes to request (default: "openid profile email").
|
|
49
|
+
"""
|
|
50
|
+
self.client_id = client_id
|
|
51
|
+
self.idp_url = idp_url.rstrip("/")
|
|
52
|
+
self.client = client
|
|
53
|
+
self.scope = scope
|
|
54
|
+
self._endpoints: Optional[Dict[str, str]] = None
|
|
55
|
+
|
|
56
|
+
async def _get_endpoints(self) -> Dict[str, str]:
|
|
57
|
+
"""
|
|
58
|
+
Discover OIDC endpoints from the IdP.
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
A dictionary containing the discovered endpoints.
|
|
62
|
+
|
|
63
|
+
Raises:
|
|
64
|
+
CoreasonIdentityError: If OIDC discovery fails.
|
|
65
|
+
"""
|
|
66
|
+
if self._endpoints:
|
|
67
|
+
return self._endpoints
|
|
68
|
+
|
|
69
|
+
discovery_url = f"{self.idp_url}/.well-known/openid-configuration"
|
|
70
|
+
|
|
71
|
+
try:
|
|
72
|
+
response = await self.client.get(discovery_url)
|
|
73
|
+
response.raise_for_status()
|
|
74
|
+
try:
|
|
75
|
+
config = response.json()
|
|
76
|
+
except ValueError as e:
|
|
77
|
+
raise CoreasonIdentityError(f"Invalid JSON response from OIDC discovery: {e}") from e
|
|
78
|
+
|
|
79
|
+
# Fallback to standard Auth0 paths if not in config
|
|
80
|
+
device_endpoint = config.get(
|
|
81
|
+
"device_authorization_endpoint", urljoin(f"{self.idp_url}/", "oauth/device/code")
|
|
82
|
+
)
|
|
83
|
+
token_endpoint = config.get("token_endpoint", urljoin(f"{self.idp_url}/", "oauth/token"))
|
|
84
|
+
|
|
85
|
+
self._endpoints = {
|
|
86
|
+
"device_authorization_endpoint": device_endpoint,
|
|
87
|
+
"token_endpoint": token_endpoint,
|
|
88
|
+
}
|
|
89
|
+
return self._endpoints
|
|
90
|
+
except httpx.HTTPError as e:
|
|
91
|
+
raise CoreasonIdentityError(f"Failed to discover OIDC endpoints: {e}") from e
|
|
92
|
+
|
|
93
|
+
async def initiate_flow(self, audience: Optional[str] = None) -> DeviceFlowResponse:
|
|
94
|
+
"""
|
|
95
|
+
Initiates the Device Authorization Flow.
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
audience: Optional audience for the token.
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
DeviceFlowResponse containing device_code, user_code, verification_uri, etc.
|
|
102
|
+
|
|
103
|
+
Raises:
|
|
104
|
+
CoreasonIdentityError: If the flow initiation fails or the response is invalid.
|
|
105
|
+
"""
|
|
106
|
+
endpoints = await self._get_endpoints()
|
|
107
|
+
url = endpoints["device_authorization_endpoint"]
|
|
108
|
+
|
|
109
|
+
data = {
|
|
110
|
+
"client_id": self.client_id,
|
|
111
|
+
"scope": self.scope,
|
|
112
|
+
}
|
|
113
|
+
if audience:
|
|
114
|
+
data["audience"] = audience
|
|
115
|
+
|
|
116
|
+
try:
|
|
117
|
+
response = await self.client.post(url, data=data)
|
|
118
|
+
response.raise_for_status()
|
|
119
|
+
try:
|
|
120
|
+
resp_data = response.json()
|
|
121
|
+
except ValueError as e:
|
|
122
|
+
raise CoreasonIdentityError(f"Invalid JSON response from initiate flow: {e}") from e
|
|
123
|
+
return DeviceFlowResponse(**resp_data)
|
|
124
|
+
except httpx.HTTPError as e:
|
|
125
|
+
logger.error(f"Device flow initiation failed: {e}")
|
|
126
|
+
raise CoreasonIdentityError(f"Failed to initiate device flow: {e}") from e
|
|
127
|
+
except ValidationError as e:
|
|
128
|
+
logger.error(f"Invalid response from device flow init: {e}")
|
|
129
|
+
raise CoreasonIdentityError(f"Invalid response from IdP: {e}") from e
|
|
130
|
+
|
|
131
|
+
async def poll_token(self, device_response: DeviceFlowResponse) -> TokenResponse:
|
|
132
|
+
"""
|
|
133
|
+
Polls the token endpoint until the user authorizes the device or the code expires.
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
device_response: The response from initiate_flow.
|
|
137
|
+
|
|
138
|
+
Returns:
|
|
139
|
+
TokenResponse containing access_token, refresh_token, etc.
|
|
140
|
+
|
|
141
|
+
Raises:
|
|
142
|
+
CoreasonIdentityError: If polling fails, times out, or the request is denied.
|
|
143
|
+
"""
|
|
144
|
+
endpoints = await self._get_endpoints()
|
|
145
|
+
url = endpoints["token_endpoint"]
|
|
146
|
+
device_code = device_response.device_code
|
|
147
|
+
interval = device_response.interval
|
|
148
|
+
expires_in = device_response.expires_in
|
|
149
|
+
|
|
150
|
+
start_time = time.time()
|
|
151
|
+
end_time = start_time + expires_in
|
|
152
|
+
|
|
153
|
+
logger.info(f"Polling for token. Expires in {expires_in}s. Interval: {interval}s")
|
|
154
|
+
|
|
155
|
+
while time.time() < end_time:
|
|
156
|
+
data = {
|
|
157
|
+
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
|
|
158
|
+
"device_code": device_code,
|
|
159
|
+
"client_id": self.client_id,
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
try:
|
|
163
|
+
response = await self.client.post(url, data=data)
|
|
164
|
+
|
|
165
|
+
if response.status_code == 200:
|
|
166
|
+
try:
|
|
167
|
+
logger.info("Token retrieved successfully.")
|
|
168
|
+
return TokenResponse(**response.json())
|
|
169
|
+
except ValidationError as e:
|
|
170
|
+
raise CoreasonIdentityError(f"Received invalid token response structure: {e}") from e
|
|
171
|
+
except ValueError as e:
|
|
172
|
+
raise CoreasonIdentityError(f"Received invalid JSON response on 200 OK: {e}") from e
|
|
173
|
+
|
|
174
|
+
# Handle errors
|
|
175
|
+
try:
|
|
176
|
+
error_resp = response.json()
|
|
177
|
+
except ValueError as e:
|
|
178
|
+
# Non-JSON response, likely a server error or proxy issue
|
|
179
|
+
response.raise_for_status()
|
|
180
|
+
raise CoreasonIdentityError(f"Received invalid response: {response.text}") from e
|
|
181
|
+
|
|
182
|
+
if not isinstance(error_resp, dict):
|
|
183
|
+
raise CoreasonIdentityError(f"Received invalid JSON response: {error_resp}")
|
|
184
|
+
|
|
185
|
+
error = error_resp.get("error")
|
|
186
|
+
|
|
187
|
+
if error == "authorization_pending":
|
|
188
|
+
pass # Continue polling
|
|
189
|
+
elif error == "slow_down":
|
|
190
|
+
interval += 5 # Increase interval as per spec
|
|
191
|
+
logger.debug("Received slow_down, increasing interval.")
|
|
192
|
+
elif error == "expired_token":
|
|
193
|
+
raise CoreasonIdentityError("Device code expired.")
|
|
194
|
+
elif error == "access_denied":
|
|
195
|
+
raise CoreasonIdentityError("User denied access.")
|
|
196
|
+
else:
|
|
197
|
+
response.raise_for_status() # Raise for other 4xx/5xx
|
|
198
|
+
|
|
199
|
+
except httpx.HTTPStatusError as e:
|
|
200
|
+
logger.error(f"Polling failed with status {e.response.status_code}: {e}")
|
|
201
|
+
raise CoreasonIdentityError(f"Polling failed: {e}") from e
|
|
202
|
+
|
|
203
|
+
except Exception as e:
|
|
204
|
+
if isinstance(e, CoreasonIdentityError):
|
|
205
|
+
raise
|
|
206
|
+
logger.warning(f"Polling attempt failed: {e}")
|
|
207
|
+
# Continue polling unless it's a critical error
|
|
208
|
+
pass
|
|
209
|
+
|
|
210
|
+
# Use anyio.sleep for async non-blocking sleep
|
|
211
|
+
await anyio.sleep(interval)
|
|
212
|
+
|
|
213
|
+
raise CoreasonIdentityError("Polling timed out.")
|