Skip to content

Commit

Permalink
feat: set username from claims
Browse files Browse the repository at this point in the history
  • Loading branch information
revant committed Feb 23, 2024
1 parent 1dc4b88 commit abd7736
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Make following changes in `site_config.json` as per your setup:
- `castlecraft_first_name_key`: OIDC Claim or key to use for getting user's first name. Default is `given_name`.
- `castlecraft_full_name_key`: OIDC Claim or key to use for getting user's full name. Default is `name`.
- `castlecraft_default_roles`: Array of roles to add to user on creation. Default is `[]`.
- `castlecraft_enable_log`: If set to `1`, `Error Log` will be created on auth errors.

Note: Either set `castlecraft_auth_introspect_bearer_enabled` or `castlecraft_auth_jwt_verify_bearer_enabled`, NOT both. In case both are set, auth `castlecraft_auth_introspect_bearer_enabled` will be considered.

Expand Down
19 changes: 14 additions & 5 deletions castlecraft/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ def validate_bearer_with_introspection(token):
frappe.local.form_dict = form_dict

except Exception:
frappe.log_error(
traceback.format_exc(),
"castlecraft_bearer_auth_failed",
)
if frappe.get_conf().get("castlecraft_enable_log"):
frappe.log_error(
traceback.format_exc(),
"castlecraft_bearer_auth_failed",
)


def validate_bearer_with_jwt_verification(token):
Expand Down Expand Up @@ -233,7 +234,11 @@ def validate_bearer_with_jwt_verification(token):
frappe.local.form_dict = form_dict

except Exception:
frappe.log_error(traceback.format_exc(), "castlecraft_jwt_auth_failed")
if frappe.get_conf().get("castlecraft_enable_log"):
frappe.log_error(
traceback.format_exc(),
"castlecraft_jwt_auth_failed",
)


def create_and_save_user(body):
Expand Down Expand Up @@ -264,6 +269,10 @@ def create_and_save_user(body):
if body.get("phone_number_verified"):
user.phone = body.get("phone_number")

username = body.get(frappe.get_conf().get("castlecraft_username_key")) # noqa: E501
if username:
user.username = username

for role in frappe.get_conf().get("castlecraft_default_roles", []):
if frappe.db.get_value("Role", role, "name"):
user.append("roles", {"role": role})
Expand Down

0 comments on commit abd7736

Please sign in to comment.