Using OSS to secure your web-app / app / ...
Tags: opensource, webdev, security
Published: 2022-11-11 08:22:48

"Security is hard. You shouldn't implement it yourself.".

That's a phrase I heard a lot by now, and it's true for the most part.
Aside from that why bother with implementing the same functionality for different projects all over again.

To solve this issue often providers like [Firebase](https://firebase.google.com/), [Auth0](https://auth0.com), ... step in an provide user-management and permission-management.
These solutions work fine but get costly very quickly.
But the main 'not-selling point' for me is that they are not opensource and cannot be hosted by myself.
I like being able to have control over things I invest my time in.

There are some opensource solutions that are trying to solve this issue; one of them is [Keycloak](https://www.keycloak.org/).
[Keycloak](https://www.keycloak.org/) solves this problem well but introduces some other problems: It is very complex and custom styling is everything but easy if you want to use something like [Next.js](https://nextjs.org/) to fully integrate the Login/Registration into your webapp.

But Keycloak isn't the only player in the field.


After some searching I found [Ory](https://www.ory.sh/).
They provide several isolated applications for different auth concerns:
- [Ory Kratos](https://www.ory.sh/docs/kratos) for Identity Management
- [Ory Keto](https://www.ory.sh/docs/keto) for Permissions & Access Control based on [Zanzibar: Google’s Consistent, Global Authorization System](https://research.google/pubs/pub48190/)
- [Ory Hydra](https://www.ory.sh/docs/hydra) for providing OAuth2 and OpenID Connect functionality

You can use everything together in their cloud variant 'Ory Network' or host it yourself. It is even possible to use only some applications, as any can be used without the others.

The functionality needed for 90% of all applications can be covered by Kratos and Keto; yes even [Social sign-in](https://www.ory.sh/docs/kratos/social-signin/overview), [Multi-factor Authentication](https://www.ory.sh/docs/kratos/mfa/overview) and [Passwordless via WebAuthn](https://www.ory.sh/docs/kratos/passwordless/webauthn).

What sets Ory's products apart from other opensource solutions is that their mantra is 'Bring your own UI'.
The (self-hosted) applications itself only provide an API (REST and partially gRPC).
You can use one of the (example-) UI's developed by Ory, but the normal way is to use your own UI.
More areas where Ory is different can be found in the [docs or Ory](https://www.ory.sh/docs/welcome#why-ory-is-different).

The project I'm currently working on uses Next.js; and it's [directly supported by Ory](https://www.ory.sh/nextjs/). It works flawless so far.

If you decide to use Kratos it can seem odd at first, but the design-decisions make sense if you think about them.
Kratos will provide an abstract representation of what elements are needed e.g. on the login-page / registration / user-settings.
With this abstract representation it is possible to render HTML-Elements or an App-UI dynamically.
The benefit of this technique: Adding e.g. a new Social-login is as easy as adding it in the Kratos config. Your UI will update automatically - without any code change or deployment.

You can use Kratos to store much more than just email and password.
With custom [Identity Schemas](https://www.ory.sh/docs/kratos/manage-identities/customize-identity-schema) you can define which fields are needed or possible for an Identity (= User).
There can even be different types of Identities, therefore requiring different information, at the same time.


For handling permissions and access-control Ory Keto can be used.
It is based on [Zanzibar: Google’s Consistent, Global Authorization System](https://research.google/pubs/pub48190/) and uses a graph to evaluate permissions.
Through this method it is very flexible in the permissions are organized.
Allowed access is represented as an existing path between two nodes in the graph.

Hierarchical structures can easily be realized and changed at any time.
With the right concept it is even possible to redo your entire permission-structure without the need to deploy your application again.


Maybe it's worth trying out Ory for your next project, you probably will like it ;)