dcrck

dcrck

I’m interested in creating a public API for my Phoenix channels, so that clients in external apps can subscribe to their events. Additionally, the channels should require authentication from clients for them to start receiving events.

This is definitely possible with Phoenix, and I understand that the socket is exposed at localhost:4000/socket/websocket, but most of the examples I can find on the internet are focused on joins/subscriptions within the same app, rather than from outside the app. In other words, they rely on a token that’s created when you log in, and encoded in the HTML somewhere.

Does anyone have any examples of protected Channel topics that support authentication from external apps? For example, you connect to the socket, pass your credentials in a message to authenticate, and then you can subscribe to a specific channel? I can’t seem to find any to study, even though I would think this isn’t a unique use case. Of course, this could be very straightforward or I could be ignoring something obvious, in which case feel free to correct me. Thanks in advance!

Showing Posts 1 to 10

sb8244

sb8244

Author of Real-Time Phoenix

The great thing about Channels is that you have complete control over the lifecycle. So you can use something like a Phoenix Token or a JWT out-of-the-box, or you could roll your own token exchange mechanism.

Do you currently have any mechanism for a person to register with your API? At work, we use OAuth to do so, which we then exchange for a JWT token on an edge server and pass around as JWT internally. You could do something similar where you take a token (any format, could even be an OAuth access token) and validate that against your database in the Phoenix.Socket connect/3 function.

There’s not much difference between a public and private API from an operational perspective. You need a different auth mechanism (usually), you need to make sure you don’t break the contract or existing users will be upset when their apps break, and you need to have good documentation. Other than that, I consider it business as usual.

dcrck

dcrck OP

I use Pow for API session management as described here: How to use Pow in an API — Pow v1.0.39

That means I’d need to first make a request for the token via the REST API, then pass that token into the websocket parameters. That seems like a decent approach…the tokens are short-lived for security purposes, so I’d need to refresh every once in a while.

A bunch of other WebSockets APIs I’ve used have API keys that you pass in once via a message, and you’re authenticated for the life of the connection. I guess I was looking for something more along those lines, but the above could work for now.

sb8244

sb8244

Author of Real-Time Phoenix

You can take that approach too—although that’s not the point I tried to get across. If you go that route, you could remove the max age check (or set it very high) on the token verifier and it will change how long that token is good for.

I would personally not recommend connecting your public API credentials with your auth system. I’d recommend that you have a dedicated set of API access tokens that can be distinctly life cycled. The most simple example of this would be a table containing a hashed API token that associates to the user. When an API token is passed to your API, you check that the token is valid and then allow it. A more advanced implementation is something like an oauth2 server that has access and refresh tokens with different life spans

dcrck

dcrck OP

Unless I’m reading it incorrectly, the example above – which I’m using – encourages that. You authenticate with your credentials, it gives you an access token and a renew token, and you make all future requests using that token as your authorization. It stores the hashed token in a cache (Mnesia in my case). It uses the cache to check and make sure the token you’re passing is valid.

sb8244

sb8244

Author of Real-Time Phoenix

Maybe it’s a difference in how we define “public API”. That flow sounds like a normal “private API” flow for an app.

I typically view a public API as being one where you need to request a specific access token, you can delete it or make new ones, and they’re not tied to your logged in session at all.

One benefit of keeping them apart would be to consider if you needed to roll your user sessions. Hopefully it’s something you wouldn’t need to do often, but it happens. If you roll it via the same mechanism that access tokens are stored in, then it would break integrations. If it’s a separate mechanism, then you could treat them independently. Pow’s mechanisms may still allow you to treat them separate, but I think it generally uses one cache and data store for all credentials.

Pow may work for the task, but I’m not sure it would fit in my use cases.

dcrck

dcrck OP

What you’re saying makes sense. Basically you’re recommending I separate the API access tokens from my app’s session storage. That way I can roll sessions without affecting existing API tokens. I’d need to build a separate value store for that, of course, but it would work very similarly to the one I have set up now. Is that correct?

If so, that’s definitely something I’ll have to put on the list of things to do. Honestly, I don’t intend for actual users to subscribe to the sockets, it’s just for my other apps to track when messages get broadcast. So I don’t think there’s a huge concern to separate right now. If this solution works well, separating shouldn’t be too tough in the future when I’m the only user. :slight_smile:

sb8244

sb8244

Author of Real-Time Phoenix

Word—if you’re just setting up your other apps to subscribe to the sockets, then I would go for the much lighter weight solution. Something like rolling credentials doesn’t really matter when it’s all in your control, because you know what to expect and can mitigate or ignore as necessary.

The solution I laid out would be for the type of system like when you setup auth with Github (which has access tokens and OAuth apps separated from other access).

dcrck

dcrck OP

Good to know. I mean, I might plan to let users know about that feature down the line, so that solution may come in handy.

dcrck

dcrck OP

@sb8244 My project’s requirements have grown; I’m ready to look into creating a separate store for API tokens. Do you know/recommend any open-source projects that implement that? They don’t have to be Elixir projects, I’m just looking for examples to study. Thanks!

sb8244

sb8244

Author of Real-Time Phoenix

I’ve only ever done this with Ruby. Since we use a microservice architecture at work, our user and identity service is in Ruby with doorkeeper.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews