-
Notifications
You must be signed in to change notification settings - Fork 12
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
e6db8d4
commit b8aa973
Showing
5 changed files
with
332 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import datetime | ||
from workos.resources.base import WorkOSBaseResource | ||
|
||
|
||
class MockSession(WorkOSBaseResource): | ||
def __init__(self, id): | ||
self.id = id | ||
self.token = "session_token_123abc" | ||
self.authorized_organizations = [ | ||
{ | ||
"organization": { | ||
"id": "org_01E4ZCR3C56J083X43JQXF3JK5", | ||
"name": "Foo Corp", | ||
} | ||
} | ||
] | ||
self.unauthorized_organizations = [ | ||
{ | ||
"organization": { | ||
"id": "org_01H7BA9A1YY5RGBTP1HYKVJPNC", | ||
"name": "Bar Corp", | ||
}, | ||
"reasons": [ | ||
{ | ||
"type": "authentication_method_required", | ||
"allowed_authentication_methods": ["GoogleOauth"], | ||
} | ||
], | ||
} | ||
] | ||
self.created_at = datetime.datetime.now() | ||
self.updated_at = datetime.datetime.now() | ||
|
||
OBJECT_FIELDS = [ | ||
"id", | ||
"token", | ||
"authorized_organizations", | ||
"unauthorized_organizations", | ||
"created_at", | ||
"updated_at", | ||
] |
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,34 @@ | ||
from workos.resources.base import WorkOSBaseResource | ||
from workos.resources.session import WorkOSSession | ||
from workos.resources.users import WorkOSUser | ||
|
||
|
||
class WorkOSAuthenticationResponse(WorkOSBaseResource): | ||
"""Representation of a User and Session response as returned by WorkOS through User Management features.""" | ||
|
||
@classmethod | ||
def construct_from_response(cls, response): | ||
authentication_response = super( | ||
WorkOSAuthenticationResponse, cls | ||
).construct_from_response(response) | ||
|
||
user = WorkOSUser.construct_from_response(response["user"]) | ||
authentication_response.user = user | ||
|
||
session = WorkOSSession.construct_from_response(response["session"]) | ||
authentication_response.session = session | ||
|
||
return authentication_response | ||
|
||
def to_dict(self): | ||
authentication_response_dict = super( | ||
WorkOSAuthenticationResponse, self | ||
).to_dict() | ||
|
||
user_dict = self.user.to_dict() | ||
authentication_response_dict["user"] = user_dict | ||
|
||
session_dict = self.session.to_dict() | ||
authentication_response_dict["session"] = session_dict | ||
|
||
return authentication_response_dict |
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,159 @@ | ||
from workos.resources.base import WorkOSBaseResource | ||
|
||
|
||
class WorkOSUserOrganization(WorkOSBaseResource): | ||
"""Contains the id and name of the associated Organization. | ||
Attributes: | ||
OBJECT_FIELDS (list): List of fields a WorkOSUserOrganization comprises. | ||
""" | ||
|
||
OBJECT_FIELDS = [ | ||
"id", | ||
"name", | ||
] | ||
|
||
|
||
class WorkOSUnauthorizedOrganizationReason(WorkOSBaseResource): | ||
"""Contains the id and name of the associated Organization. | ||
Attributes: | ||
OBJECT_FIELDS (list): List of fields a WorkOSUnauthorizedOrganizationReason comprises. | ||
""" | ||
|
||
OBJECT_FIELDS = [ | ||
"type", | ||
"allowed_authentication_methods", | ||
] | ||
|
||
|
||
class WorkOSAuthorizedOrganization(WorkOSBaseResource): | ||
"""Contains the id and name of the associated Organization. | ||
Attributes: | ||
OBJECT_FIELDS (list): List of fields a WorkOSUserOrganization comprises. | ||
""" | ||
|
||
@classmethod | ||
def construct_from_response(cls, response): | ||
authorized_organization = super( | ||
WorkOSAuthorizedOrganization, cls | ||
).construct_from_response(response) | ||
|
||
organization = WorkOSUserOrganization.construct_from_response( | ||
response["organization"] | ||
) | ||
authorized_organization.organization = organization | ||
|
||
return authorized_organization | ||
|
||
def to_dict(self): | ||
authorized_organizations_dict = super( | ||
WorkOSAuthorizedOrganization, self | ||
).to_dict() | ||
|
||
organization_dict = self.organization.to_dict() | ||
authorized_organizations_dict["organization"] = organization_dict | ||
|
||
return authorized_organizations_dict | ||
|
||
|
||
class WorkOSUnauthorizedOrganization(WorkOSBaseResource): | ||
"""Contains the unauthorized organization and reasons for the non authorization. | ||
Attributes: | ||
OBJECT_FIELDS (list): List of fields a WorkOSUserOrganization comprises. | ||
""" | ||
|
||
@classmethod | ||
def construct_from_response(cls, response): | ||
unauthorized_organization = super( | ||
WorkOSUnauthorizedOrganization, cls | ||
).construct_from_response(response) | ||
|
||
organization = WorkOSUserOrganization.construct_from_response( | ||
response["organization"] | ||
) | ||
unauthorized_organization.organization = organization | ||
|
||
unauthorized_organization.reason = [] | ||
|
||
for reason in response["reasons"]: | ||
reason = WorkOSUnauthorizedOrganizationReason.construct_from_response( | ||
reason | ||
) | ||
unauthorized_organization.reason.append(reason) | ||
|
||
return unauthorized_organization | ||
|
||
def to_dict(self): | ||
unauthorized_organizations_dict = super( | ||
WorkOSUnauthorizedOrganization, self | ||
).to_dict() | ||
|
||
organization_dict = self.organization.to_dict() | ||
unauthorized_organizations_dict["organization"] = organization_dict | ||
|
||
unauthorized_organizations_dict["reasons"] = [] | ||
|
||
for reason in self.reason: | ||
reason_dict = reason.to_dict() | ||
unauthorized_organizations_dict["reasons"].append(reason_dict) | ||
|
||
return unauthorized_organizations_dict | ||
|
||
|
||
class WorkOSSession(WorkOSBaseResource): | ||
"""Representation of a Session response as returned by WorkOS through User Management features. | ||
Attributes: | ||
OBJECT_FIELDS (list): List of fields a class WorkOSSession comprises. | ||
""" | ||
|
||
OBJECT_FIELDS = [ | ||
"id", | ||
"token", | ||
"created_at", | ||
"updated_at", | ||
] | ||
|
||
@classmethod | ||
def construct_from_response(cls, response): | ||
session = super(WorkOSSession, cls).construct_from_response(response) | ||
|
||
session.authorized_organizations = [] | ||
session.unauthorized_organizations = [] | ||
|
||
for authorized_organization in response["authorized_organizations"]: | ||
authorized_organization = ( | ||
WorkOSAuthorizedOrganization.construct_from_response( | ||
authorized_organization | ||
) | ||
) | ||
session.authorized_organizations.append(authorized_organization) | ||
|
||
for unauthorized_organization in response["unauthorized_organizations"]: | ||
unauthorized_organization = ( | ||
WorkOSUnauthorizedOrganization.construct_from_response( | ||
unauthorized_organization | ||
) | ||
) | ||
session.unauthorized_organizations.append(unauthorized_organization) | ||
|
||
return session | ||
|
||
def to_dict(self): | ||
session_dict = super(WorkOSSession, self).to_dict() | ||
|
||
session_dict["authorized_organizations"] = [] | ||
session_dict["unauthorized_organizations"] = [] | ||
|
||
for organization in self.authorized_organizations: | ||
organization_dict = organization.to_dict() | ||
session_dict["authorized_organizations"].append(organization_dict) | ||
|
||
for organization in self.unauthorized_organizations: | ||
organization_dict = organization.to_dict() | ||
session_dict["unauthorized_organizations"].append(organization_dict) | ||
|
||
return session_dict |
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