Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ory Kratos Integration. #198

Merged
merged 12 commits into from
Feb 5, 2024
Merged

Add Ory Kratos Integration. #198

merged 12 commits into from
Feb 5, 2024

Conversation

Shaktizala
Copy link
Contributor

Closes #193

Changes proposed

  • Kratos Integration
  • Endpoint for Kratos redirecting and storing user details in the database.
  • Kratos service in docker compose and kratos database in the same PostgreSQL server.

Check List (Check all the applicable boxes)

  • My code follows the code style of this project.
  • My change requires changes to the documentation.
  • I have updated the documentation accordingly.
  • All new and existing tests passed.
  • This PR does not contain plagiarized content.
  • The title of my pull request is a short description of the requested changes.

- kratos database in same postgres server with backend database configuration is in the docker-compose.yaml
- add auth controller for the kratos only if the kratos required flag is set to true
- change in the user table schema, password and roles are nullable now
- kratos_id addition and model for the same for databae insertion
@Shaktizala Shaktizala self-assigned this Jan 16, 2024
README.md Outdated Show resolved Hide resolved
config/kratos.go Outdated
BaseURL string `envconfig:"SERVE_PUBLIC_BASE_URL"`
UIUrl string `envconfig:"SELF_SERVICE_DEFAULT_BROWSER_RETURN_URL"`
AdminUrl string `envconfig:"SERVE_ADMIN_BASE_URL"`
PublicUrl string `envconfig:"SERVE_PUBLIC_BASE_URL"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shaktizala What is different between BaseURL and PublicURL? Also keep consistency in naming. In BaseURL, URL is in uppercase when in PublicUrl and AdminUrl it is in CamelCase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both are same, it's the kratos base url, but as kratos requires envs to be in specific format it is kept like, "SERVE_PUBLIC_BASE_URL".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shaktizala If both are same then why are you using two variables for same env?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, it was there for previous env configuration, will remove it.

constants/constant.go Outdated Show resolved Hide resolved
// 400: GenericResFailBadRequest
// 500: GenericResError
func (ctrl *AuthController) DoKratosAuth(c *fiber.Ctx) error {
kratosID := c.Locals(constants.KratosID)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shaktizala What is the purpose of using c.Locals?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used to fetch kratosID into the next handle. It means we can add it to locals from here and fetch it from the next handler.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shaktizala Is it middleware? Middleware shouldn't be in controller

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not middleware. used to pass an ID from middleware to next handler.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, is DoKratosAuth middleware?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not middleware. It's an endpoint on which kratos will redirect after successful login/registration.

controllers/api/v1/auth_controller.go Outdated Show resolved Hide resolved
controllers/api/v1/auth_controller.go Outdated Show resolved Hide resolved
config/kratos.go Outdated Show resolved Hide resolved
route.Get("/\<end-point\>", middlewares.Authenticated, authController.DoKratosAuth)

## How Kratos Integration Works?
You have to provide Kratos with your UI endpoint URLs, Kratos will redirect to those endpoints by initializing the flow.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shaktizala What frontend has to sent? Should it work with ajax request? Does it need to follow any xml/json format? Or it is form?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is in form.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then please document that or add link of external doc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay will update it.

Shaktizala and others added 3 commits January 16, 2024 15:08
Co-authored-by: Munir Khakhi <6319375+munir131@users.noreply.github.com>
Signed-off-by: Shaktiraj Zala <123356347+Shaktizala@users.noreply.github.com>
Co-authored-by: Munir Khakhi <6319375+munir131@users.noreply.github.com>
Signed-off-by: Shaktiraj Zala <123356347+Shaktizala@users.noreply.github.com>
c.Redirect(ctrl.config.Kratos.UIUrl)

c.Locals(constants.KratosUserDetails, user)
c.Next()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shaktizala What is purpose of it? L154 will already redirect on UI

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, forgot to remove it, it was there when I was not doing redirection. will update it.

munir131
munir131 previously approved these changes Jan 23, 2024
Copy link

@pratikbgit pratikbgit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Define step pkg/kratos/oidc then run base64 -w 0 google.schema.jsonnet
  • Mention the steps for OAuth 2.0 client IDs and redirect URLs set.
  • Start service in on go, like Kratos up command then migrate up then api run etc.
  • Info: If the password field is not required, then please remove it.

@Shaktizala
Copy link
Contributor Author

Shaktizala commented Jan 23, 2024

  • Define step pkg/kratos/oidc than run base64 -w 0 google.schema.jsonnet
  • Mention steps of OAuth 2.0 Client IDs and redirect url set.
  • Start service in on go like Kratos up command then migrate up then api run etc.
  • info : If password filed is not required then please remove it.

@pratikbgit Password field is required, when kratos is not enabled We have to use authentication using password so at that time we need to store password.

I am not getting the 3rd statement, can you please elaborate.

CC: @munir131

@pratikbgit
Copy link

pratikbgit commented Jan 23, 2024

  • Define step pkg/kratos/oidc than run base64 -w 0 google.schema.jsonnet
  • Mention steps of OAuth 2.0 Client IDs and redirect url set.
  • Start service in on go like Kratos up command then migrate up then api run etc.
  • info : If password filed is not required then please remove it.

@pratikbgit Password field is required, when kratos is not enabled We have to use authentication using password so at that time we need to store password.

I am not getting the 3rd statement, can you please elaborate.

CC: @munir131

While kratos service start before that migrate and api service should start not manually.

@Shaktizala
Copy link
Contributor Author

Shaktizala commented Jan 30, 2024

  • Define step pkg/kratos/oidc than run base64 -w 0 google.schema.jsonnet
  • Mention steps of OAuth 2.0 Client IDs and redirect url set.
  • Start service in on go like Kratos up command then migrate up then api run etc.
  • info : If password filed is not required then please remove it.

@pratikbgit Password field is required, when kratos is not enabled We have to use authentication using password so at that time we need to store password.
I am not getting the 3rd statement, can you please elaborate.
CC: @munir131

While kratos service start before that migrate and api service should start not manually.

@pratikbgit
I discussed this with @munir131.

  • We will have to make one script file for starting all three services.
  • There is no need to remove the password field, as it is required when we are not using Kratos.
  • I will add a link to the steps of creating a client ID, secrets, and different details of it.

munir131
munir131 previously approved these changes Feb 5, 2024
@munir131
Copy link
Member

munir131 commented Feb 5, 2024

@Shaktizala Please resolve conflicts

Signed-off-by: Shaktiraj Zala <123356347+Shaktizala@users.noreply.github.com>
@munir131 munir131 merged commit 3e560c1 into main Feb 5, 2024
2 checks passed
@munir131 munir131 deleted the feat/kratos-integration branch February 5, 2024 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ Task ] Add integration of Kratos User Identity management system.
3 participants