emojicon authentications
We all could use a little more emotion in our lives 🤗 Go show the world how you really feel!
It's a great companion for any app that uses 2FA - as every app should! Make your app's 2FA as star-striking as your app! 🤩
So whether your mascot is:
- 🦋
- 🐰
- 🐶
- 🐕
- 🐷
- 🐥
- 🐱
- 🐣
- 🐻
- 🐈
moths has got you covered! 🪰
P.S. These emojies wasn't chosen by accident (random), they really are the 10 utmost frequently used (animal) emojies of 2021
Show that sparkly emojies ✨
Running this is quite easy 💨
- Download the dependencies ⏬
Note
You can skip this step and let the
run-command handle it - but you knew that 🧠
go mod download- Add a secret 🔐
Warning
This is just an one-liner to write the
base64-encodedsha256ofmothsinto.env. Make sure to use something better 🤷You don't need to use environment variables, this is just my choice.
echo 'moths' | sha256sum | base64 -w 0 | { echo -n "MOTHS_SECRET=$(cat -)" } > .env- Run the program 🏃
go run .As I said, easy-peasy! 💖
To setup a new code-generator, you must call token.NewGenerator as
gen, err := token.NewGenerator()But I do recommend to add some customization, as it will fail immediately if not.
Like so
gen, err := token.NewGenerator(
token.OptionWithSecret(secret), // Any string as the secret
token.OptionWithPeriod(time.Second), // Each code is only valid for a second
token.OptionWithAmount(6), // Each must is always `6` emojies
token.OptionWithEmojies(emojies.CATS), // A pure slice of cats
token.OptionWithTime(time.Now().AddDate(10, 0, 0)), // 10 years into the future
)There are a few options to choose from, these are
OptionWithSecret(secret string)- The secret to generate from
- Defaults to a random 32-character string
OptionWithPeriod(period time.Duration)*- On which interval should a new
codebe generated - A
codewill only be valid during this duration - Until (if) skewed interval are implemented
- On which interval should a new
OptionWithAmount(amount int)- The amount of emojies to generate in a
moth - Defaults to
6-charater tokens
- The amount of emojies to generate in a
OptionWithEmojies(emojies emojies.Emojies)*OptionWithTime(t time.Time)- This will allow you to add a custom time
- Meaning you can validate towards old
codes - You can even add future dates ⌛
- Defaults to now
OptionFromEncoded(str string)- Use this if you have exported a generator earlier
Warning
Options marked with an asterix (*) are required!
Now that you have a sparkly new code-generator, you can use it as
code, err := gen.Next()Now that you have a code, you can use its functions
Validate(emojies string) bool- Will validate a code (pattern of emojies) directly
ValidateToken(token string) bool- DEPRECATED- Will validate a token
- You'll need to expose the token to your user(s) for this - not recommended
String() string- Returns the code as a string
SpacedString() string- Returns the code as a string with spaces inbetween
Slice() []string- Returns the code as a slice of strings
- Returns the code as a string
CreatedAt() time.Time- Returns the time the code was generated
ExpiresAt() time.Time- Returns the time when the code will expire
Token() string- DEPRECATED- Returns the token - for whatever reason that might be needed
To validate, you'll need both the code (or the token) and the generator
str := code.String() // Ideally you'd get this from the user
ok := gen.Validate(str)To use your own set of known emojies, you can reference the cat-emojies.
I've chosen the cats as it's a great reference, both for creating and re-using.
If we would like to add an easter-egg to a purebred slice of cats, we could do it like:
// A slice of cat emojies and a single hotdog 🌭
var catsHotdog = []string{
emoji.GrinningCat.String(),
emoji.GrinningCatWithSmilingEyes.String(),
emoji.CatWithTearsOfJoy.String(),
emoji.SmilingCatWithHeartEyes.String(),
emoji.CatWithWrySmile.String(),
emoji.KissingCat.String(),
emoji.WearyCat.String(),
emoji.CryingCat.String(),
emoji.PoutingCat.String(),
emoji.HotDog.String(),
}
var CATS_HOTDOG = ToEmojies(catsHotdog)It doesn't even have to be from the emoji-package,
simply make a slice of your desired emojies, and use the ToEmojies-func.
Then provide this as an argument in token.OptionWithEmojies(CATS_HOTDOG) when calling the token.NewGenerator().
Check out main.go for an example
-
v6.0.0💘
- Add Skew Intervals 🕰️
- Add better documentation 🫢
- Get into fixing
geatures🤦 - d1bb4ccExport/ImportRename- 6105848mothto something better (OTP/Token)Rename the- 4c973efMoths-struct toGeneratorThis means renaming the- fcdf295otp-package as well?
Get pkg.go.dev up-to-date- Works with v6
I couldn't have done it without these lovely OSS 🦾
- https://github.com/aidarkhanov/nanoid
- https://github.com/enescakir/emoji
- https://github.com/tilaklodha/google-authenticator
- https://github.com/pquerna/otp/
In no specific order 🤷
