Skip to content

Commit

Permalink
Merge pull request #706 from shimilgithub/api-refactor
Browse files Browse the repository at this point in the history
Upgrade Kratos to v1.0.0
  • Loading branch information
kavitharaju authored Sep 22, 2023
2 parents f8d3940 + 4862e88 commit b8956da
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 45 deletions.
12 changes: 8 additions & 4 deletions app/auth/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,13 @@ def get_users_kratos_filter(base_url,name,roles,limit,skip):#pylint: disable=too
"userId":data["id"],
"name":data["traits"]["name"]
}
kratos_user["name"]["fullname"] = data["traits"]["name"]["first"].capitalize() \
+ " "+ data["traits"]["name"]["last"].capitalize()

first_name = data["traits"]["name"]["first"].capitalize()
last_name = data["traits"]["name"].get("last", "")
last_name = last_name.capitalize() if last_name else last_name
kratos_user["name"]["fullname"] = first_name+(" " + last_name if last_name else "")
if not name is None:
if name.lower() == kratos_user["name"]["fullname"].lower() or\
name.lower() == kratos_user["name"]["last"].lower() or\
name.lower() == kratos_user["name"]["first"].lower():
name_status = True
else:
Expand Down Expand Up @@ -486,12 +488,14 @@ def update_kratos_user(rec_user_id,data):
def register_check_success(reg_response):
"""register reqirement success"""
name_path = reg_response["identity"]["traits"]["name"]
last_name = name_path.get("last", "")
data={
"message":"Registration Successfull",
"registered_details":{
"id":reg_response["identity"]["id"],
"email":reg_response["identity"]["traits"]["email"],
"Name":str(name_path["first"]) + " " + str(name_path["last"]),
"Name": str(name_path["first"]) + (" " + last_name if last_name else ""),

"Permissions": reg_response["identity"]["traits"]["userrole"]
},
"token":reg_response["session_token"]
Expand Down
2 changes: 1 addition & 1 deletion app/schema/schema_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Registration(BaseModel):
"""kratos registration input"""
email:str
password:types.SecretStr
firstname:str = None
firstname:str
lastname:str = None

class EditUser(BaseModel):
Expand Down
53 changes: 30 additions & 23 deletions app/test/test_auth_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def login(data):
if response.status_code == 200:
assert response.json()['message'] == "Login Succesfull"
token = response.json()['token']
assert len(token) == 32
assert len(token) == 39
assert "userId" in response.json()
elif response.status_code == 401:
assert response.json()['error'] == "Authentication Error"
Expand All @@ -63,7 +63,7 @@ def register(data,apptype):
assert "Permissions" in response.json()["registered_details"]
assert "token" in response.json()
token = response.json()['token']
assert len(token) == 32
assert len(token) == 39
return response

#appending roles to same user on duplicate registration
Expand Down Expand Up @@ -213,7 +213,9 @@ def test_incorrect_email():
"""test for validation of incorrect email"""
data = {
"email": "incorrectemail",
"password": "passwordabc@1"
"password": "passwordabc@1",
"firstname": "user registration",
"lastname": "ABCD Test"
}
response = register(data,apptype=schema_auth.App.API.value)
assert response.status_code == 422
Expand All @@ -225,7 +227,9 @@ def test_validate_password():
#short password
data = {
"email": "[email protected]",
"password": "test"
"password": "test",
"firstname": "user registration",
"lastname": "PQR Test"
}
response = register(data,apptype=schema_auth.App.API.value)
assert response.status_code == 422
Expand All @@ -234,7 +238,9 @@ def test_validate_password():
#less secure password
data = {
"email": "[email protected]",
"password": "password"
"password": "password",
"firstname": "user registration",
"lastname": "PQR Test"
}
response = register(data,apptype=schema_auth.App.API.value)
assert response.status_code == 422
Expand All @@ -243,29 +249,20 @@ def test_validate_password():
#test for optional params in registration
def test_optional_register_params(create_user_fixture):
"""test for optional params in the registration"""
#app type is none
#app type is none and lastname is not passed
data = {
"email": "[email protected]",
"password": "passwordabc@1",
"firstname": "user registration",
"lastname": "ABC Test"
"email": "[email protected]",
"password": "passwordabc@11",
"firstname": "user registration"
}
response = register(data,apptype=schema_auth.App.API.value)
assert response.json()["registered_details"]["Permissions"] == \
[schema_auth.App.API.value]
abc_id = response.json()["registered_details"]["id"]

#no first and last name, registration execute without error
data = {
"email": "[email protected]",
"password": "passwordabc@1"
}
response1 = register(data,apptype=schema_auth.App.API.value)
abc1_id = response1.json()["registered_details"]["id"]

users_list = create_user_fixture
users_list.append(abc_id)
users_list.append(abc1_id)

#test register with missing field
def test_register_incorrectdatas():
Expand Down Expand Up @@ -384,7 +381,9 @@ def test_register_roles(create_user_fixture):
# #role changed ag --> vachan
data_xyz2 = {
"email": "[email protected]",
"password": "passwordxyz2@1"
"password": "passwordxyz2@1",
"firstname": "xyz user 2",
"lastname": "xyz Test 2"
}
response2 = register_role_appending(data_xyz2,apptype=schema_auth.App.VACHAN.value)
assert response2.json()["registered_details"]["Permissions"] ==\
Expand All @@ -393,7 +392,9 @@ def test_register_roles(create_user_fixture):
#role changed none --> ag
data_xyz3 = {
"email": "[email protected]",
"password": "passwordxyz3@1"
"password": "passwordxyz3@1",
"firstname": "xyz user 3",
"lastname": "xyz Test 3"
}
response3 = register_role_appending(data_xyz3,apptype=schema_auth.App.AG.value)
assert response3.json()["registered_details"]["Permissions"] ==\
Expand Down Expand Up @@ -423,15 +424,19 @@ def test_role_assignment_superadmin(create_user_fixture):
#create 2 users
user1 = {
"email": "[email protected]",
"password": "passwordvachan@1"
"password": "passwordvachan@1",
"firstname": "vachan",
"lastname": "User Test"
}
response1 = register(user1,apptype=schema_auth.App.API.value)
user1_id = response1.json()["registered_details"]["id"]
assert response1.json()["registered_details"]["Permissions"] == [schema_auth.App.API.value]

user2 = {
"email": "[email protected]",
"password": "passwordag@1"
"password": "passwordag@1",
"firstname": "Ag",
"lastname": "User Test"
}
response2 = register(user2,apptype=schema_auth.App.API.value)
user2_id = response2.json()["registered_details"]["id"]
Expand Down Expand Up @@ -494,7 +499,9 @@ def test_token_expiry(create_user_fixture):
#try change role with super user after logout
user = {
"email": "[email protected]",
"password": "passworduser@1"
"password": "passworduser@1",
"firstname": "user ",
"lastname": "role change Test"
}
response2 = register(user,apptype=schema_auth.App.API.value)
user_id = response2.json()["registered_details"]["id"]
Expand Down
2 changes: 1 addition & 1 deletion docker/Kratos_config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3.7"

services:
kratos-migrate:
image: oryd/kratos:v0.7.0-alpha.1
image: oryd/kratos:v1.0.0
environment:
- DSN=${VACHAN_AUTH_DATABASE:-postgres://kratos:secret@postgresd:5432/kratos?sslmode=disable&max_conns=20&max_idle_conns=4}
volumes:
Expand Down
25 changes: 14 additions & 11 deletions docker/Kratos_config/email-password/kratos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ serve:
base_url: http://kratos:4434/

selfservice:
default_browser_return_url: http://127.0.0.1:4455/
whitelisted_return_urls:
- http://127.0.0.1:4455
default_browser_return_url: http://kratos:4455/
allowed_return_urls:
- http://kratos:4455

methods:
password:
Expand All @@ -23,33 +23,33 @@ selfservice:

flows:
error:
ui_url: http://127.0.0.1:4455/error
ui_url: http://kratos:4455/error

settings:
ui_url: http://127.0.0.1:4455/settings
ui_url: http://kratos:4455/settings
privileged_session_max_age: 15m

recovery:
enabled: true
ui_url: http://127.0.0.1:4455/recovery
ui_url: http://kratos:4455/recovery

verification:
enabled: true
ui_url: http://127.0.0.1:4455/verify
ui_url: http://kratos:4455/verify
after:
default_browser_return_url: https://api.vachanengine.org/

logout:
after:
default_browser_return_url: http://127.0.0.1:4455/auth/login
default_browser_return_url: http://kratos:4455/auth/login

login:
ui_url: http://127.0.0.1:4455/auth/login
ui_url: http://kratos:4455/auth/login
lifespan: 2m

registration:
lifespan: 10m
ui_url: http://127.0.0.1:4455/auth/registration
ui_url: http://kratos:4455/auth/registration
after:
password:
hooks:
Expand Down Expand Up @@ -77,7 +77,10 @@ session:
lifespan: 60m

identity:
default_schema_url: file:///etc/config/kratos/identity.schema.json
default_schema_id: default
schemas:
- id: default
url: file:///etc/config/kratos/identity.schema.json

# courier:
# smtp:
Expand Down
6 changes: 3 additions & 3 deletions docker/Kratos_config/quickstart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3.7'

services:
kratos-migrate:
image: oryd/kratos:v0.7.0-alpha.1
image: oryd/kratos:v1.0.0
environment:
- DSN=postgres://kratos:secret@postgresd:5432/kratos?sslmode=disable&max_conns=20&max_idle_conns=4
volumes:
Expand All @@ -19,7 +19,7 @@ services:
- intranet

kratos-selfservice-ui-node:
image: oryd/kratos-selfservice-ui-node:v0.7.0-alpha.1
image: oryd/kratos-selfservice-ui-node:v1.0.0
environment:
- KRATOS_PUBLIC_URL=http://kratos:4433/
- KRATOS_ADMIN_URL=http://kratos:4434/
Expand All @@ -30,7 +30,7 @@ services:
kratos:
depends_on:
- kratos-migrate
image: oryd/kratos:v0.7.0-alpha.1
image: oryd/kratos:v1.0.0
ports:
- '4433:4433' # public
- '4434:4434' # admin
Expand Down
4 changes: 2 additions & 2 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ services:
- VE-network

kratos-migrate:
image: oryd/kratos:v0.7.0-alpha.1
image: oryd/kratos:v1.0.0
environment:
- DSN=${VACHAN_AUTH_DATABASE:-postgres://kratos:secret@kratos-postgresd:5432/kratos?sslmode=disable&max_conns=20&max_idle_conns=4}
volumes:
Expand All @@ -44,7 +44,7 @@ services:
- VE-network

kratos:
image: oryd/kratos:v0.7.0-alpha.1
image: oryd/kratos:v1.0.0
ports:
- '4433:4433' # public
# - '4434:4434' # admin
Expand Down

0 comments on commit b8956da

Please sign in to comment.