Tuesday, August 16, 2022
HomeiOS DevelopmentThe best way to retailer keys in env information?

The best way to retailer keys in env information?

On this tutorial I will present you the best way to save and cargo secret keys as base64 encoded strings utilizing dotenv information in Vapor 4.


Utilizing the Surroundings in Vapor 4

Identical to many well-liked server aspect frameworks, your Vapor primarily based backend software can load a file referred to as .env. It’s attainable to retailer key-value primarily based (secret) configuration values inside this file. If you run the app, one of many following file can be loaded, primarily based on the present surroundings:

  • Manufacturing (.env)
  • Growth (.env.growth)
  • Testing (.env.testing)

If you execute your checks the .env.testing file can be used. In the event you begin the app utilizing the serve Vapor command you can even change the surroundings utilizing the --env or -e flag. The accessible choices are manufacturing and growth, and the corresponding .env file can be loaded. It’s attainable to create a customized surroundings, you’ll be able to learn extra about this within the official Vapor docs. The .env file normally incorporates one key and worth per line, now the issue begins once you wish to retailer a multiline secret key within the file. So what can we do about this? 🤔

Base64 encoded secret keys

Sure, we will encode the key key utilizing a base64 encoding. No, I do not wish to copy my secrets and techniques into an on-line base64 encoder, as a result of there’s a fairly easy shell command that I can use.

echo "<my-secret-key>" | base64

In the event you don’t love unix instructions, we will all the time put collectively a bit of Swift script and use an extension on the String sort to encode keys. Simply save the snippet from under right into a base64.swift file, put your key into the important thing part, give the file some executable permission & run it utilizing the chmod o+x && ./base64.swift one-liner command and voilá…

#! /usr/bin/swift

import Basis

extension String {

    func base64Encoded() -> String? {
        return knowledge(utilizing: .utf8)?.base64EncodedString()

let key = """


You possibly can copy & paste the encoded worth of the key key into your personal .env.* file, exchange the asterix image together with your present surroundings after all, earlier than you do it. 🙈

//e.g. .env.growth

Now we simply must decode this key someway, earlier than we will begin utilizing it…

Decoding the key key

You possibly can implement a base64 decoder as a String extension with only a few traces of Swift code.

import Basis

extension String {

    func base64Decoded() -> String? {
        guard let knowledge = Information(base64Encoded: self) else { return nil }
        return String(knowledge: knowledge, encoding: .utf8)

Now in my tasks I like to increase the Surroundings object and place all my customized variables there as static constants, this manner I can entry them in a extremely handy approach, plus if one thing goes incorrect (normally after I do not re-create the .env file after a git reset or I haven’t got all of the variables current within the dotenv file) the app will crash due to the compelled unwraps, and I will know for certain that one thing is incorrect with my surroundings. It is a crash for my very own security. 💥

import Vapor

extension Surroundings {
    static let secretKey = Self.get("SECRET_KEY")!.base64Decoded()!


I believe this strategy may be very helpful. In fact you need to place the .env.* sample into your .gitignore file, in any other case in the event you place some secrets and techniques into the dotenv file and also you push that into the distant… nicely, everybody else will know your keys, passwords, and many others. You do not need that, proper? ⚠️

Be happy to make use of this technique when it’s important to implement a Sign up With Apple workflow, or a Apple Push Notification service (APNs). In these instances you will positively must cross one ore extra secret keys to your Vapor primarily based backend software. That is it for now, thanks for studying.



Please enter your comment!
Please enter your name here

Most Popular