shotleybuilder

shotleybuilder

I’ve worked through this excellent blog post and my tests pass. But, I find myself missing the ‘big picture’ / mental map that comes from having a truly working example.

http://codeloveandboards.com/blog/2018/06/20/elixir-and-phoenix-basic-passwordless-and-databaseless-authentication-pt-2/

The tutorial slips into using sockets and channels. I’d rather not have to do that. I’d like another example tutorial that picks up where this one finishes. I’d simply like to know how to secure the app!

Does the user have to provide the token with each get request? Or does the server somehow retain knowledge of this after they’ve clicked the emailed link? Where is the checking done if not in the socket?

Oh, another latent concern is getting gigalixir to send an email when I come to deploy. I haven’t Googled Bamboo in deployment yet, but I just know that’s going to be tricky to research.

The tribulations of trying to learn this stuff.

Showing Posts 1 to 6

shotleybuilder

shotleybuilder OP

I feel like I’m both treading water and going around in circles … :slight_smile:
My use case is to simply restrict access to a few domains. Hence a user can confirm who they are by clicking the emailed link. The domains are hardcoded in config.exs.
I have no front end. And no database. This is just a service providing data.
Here’s my mental map (at the mo’!) - thinking aloud.
User issues a get request containing their email as a parameter (no post requests since there is no front end).
After a simple check on email domain, the token for the domain (not the user) gets emailed as a clickable link.
The user’s cookie (which I’m assuming is on their browser but I could be wrong because Plug.Session talks about ETS too and then tutorials immediately start using Postgres or Redis) is updated with the token.
Now, when they come back at a later time their cookie is proof they’ve gone through the email authentication process.

LostKobrakai

LostKobrakai

This should really be a post. If you’re worried about no csrf – a get request is as unsafe, but without communicating that side effects might happen.

True, but it also means you have no way to invalidate this authentication. It stays valid until it expired by whatever static means of expiration you’re using.

shotleybuilder

shotleybuilder OP

Yes, I agree that it should be a post, that’s why I mentioned it. But w/o a front end I don’t think I have any other option. Logically the user is ‘getting an email’ :laughing:

Expiration. I could expire the token? By associating a new token with each domain all users would need to reconfirm.

LostKobrakai

LostKobrakai

What do you mean by “no frontend”? The user needs to somehow query your endpoint. If it’s through a browser use a form, if it’s through some other generic http library make them do a post request.

True, this is quite a sledgehammer though. Kinda like changing the session secret to invalidate all sessions.

shotleybuilder

shotleybuilder OP

:sweat_smile: Yeah, I suppose I could create a simple form and use html() to serve it. I just meant that I haven’t built Phoenix with html with all the in built helpers and such. Trying to keep things super simple - no chance!

shotleybuilder

shotleybuilder OP

In case anyone travels this path in the future this is what I did …

This tutorial was my jumping off point: Elixir and Phoenix basic passwordless and databaseless authentication (pt.2) · bigardone.dev I did find part 3 but that’s not so helpful since it covers building an Elm SPA.

My use case was restricting access to particular domains. So instead of a hard encoded list of users I have a manageable number of domains in config.exs. Even so, the number of domains might grow to be unmanageable here, and I’ll need to explore an alternative - Mnesia perhaps.

GenServer is used for state. In my case it’s an advantage that a GenServer is transient and to require users to re-register when the GenServer restarts. Maybe not so good for a public facing app, but I’m working on an in-house tool and it’s a feature :slight_smile:

The state is a nested map %{domain: %{user_name: “token”}}. The user name is randomly generated using :crypto. I cannot cancel a known users registration because I don’t have any knowledge of a particular user on the server.

Authentication is done using an emailed token. I’m using SendGrid, which was relatively painless to set-up, although a couple of module names have changed which needed fixing.

When a user submits their email address the first thing that is checked is the domain. If it’s not supported then they fall at the first fence. The successful submission and verification of the emailed link (one that didn’t display localhost!) results in 3 cookies being saved to the users browser - their random name, their org and the encrypted token. All are set to expire at 2-weeks.

Handily (I didn’t know this beforehand) the browser automatically submits the cookies for each subsequent request. They have to be fetched to populate conn. I’ve built a small plug that requests are routed through. The plug checks the org (first check), then finds their user name against the org (second check), and then checks the now unencrypted token against the user name (third check). All by calling the GenServer.

I have another layer completing authorisation by comparing the org against metadata in the data that the user requests. Rather than trying to authorise against resources ahead of time. The owners of the data can decide who gets to see it.

Users have to go through registration frequently. I’ve set-up a monthly cleanse of the GenServer. Their cookies expire after 2-weeks. And it’ll reset each time I redeploy or the GenServer falls over. Given the registration is so lightweight I regard this as a feature for my use case.

That’s it! My first GenServer, my first plug, and the first time using Bamboo and SendGrid.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews