ajur58

ajur58

Storing Bearer Token for external APIs

Hello everyone,

I’m implementing an app that consumes an external API. The API returns a Bearer Token valid for 2 hrs after logging in, to be used for every subsequent request. I’m new to Elixir and was wondering where to store this token for the future.

My current approach:

  1. Standalone application (Agent) to handle communication with this external API
  2. When the app is started, it tries to authenticate and get the AuthToken (checking expiry date etc)
  3. The token is saved in the agent’s state, it will be read from here in future calls.

Alternatives that I can think of :
a. Put the AuthToken into env variables with Application.put_env/3 → slight security concerns as if the app crashes, env variables could get dumped to the console. Also, other apps will never need this, so it feels strange.
b. Write it to DB? Could be an overkill, but at least there’ll be one entry per new token, good for tracing.
c. Write to disk.

Might be a dumb question, but would be grateful if someone helps a noob out :slight_smile:
Thanks!

Most Liked

stefanchrobot

stefanchrobot

Keeping the token in memory means that it will most likely not survive application restarts (unless you have distribution configured). If that’s an issue, I’d store it in the DB as an encrypted field. If that’s not a concern, keeping it in an Agent seems fine to me.

Note that keeping the token in memory can make it leak - if a process crashes, the current process state can get dumped to a log file. For improved security consider wrapping the token in a struct with overriden Inspect implementation or using a redacted field in Ecto.

ajur58

ajur58

Thanks for your answer! Interesting stuff about leaky process state, will definitely keep it in mind.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement