Skip to content
Permalink
Browse files

Add token generator

Unique tokens are necessary to generate a unique URL that will be used
as an user/project identifier for the free session feature.

The idea is that an unique URL is the access point for the free project
of a given user.
  • Loading branch information...
ggarciajr committed Apr 18, 2016
1 parent 68aca15 commit 35e0d7f505db9590a2f32528d29a660e3bb45c5e
Showing with 59 additions and 1 deletion.
  1. +19 −0 Helpers/Tokens.hs
  2. +22 −0 flora.cabal
  3. +1 −1 stack.yaml
  4. +17 −0 test/Helpers/TokensSpec.hs
@@ -0,0 +1,19 @@
module Helpers.Tokens (
generateToken
) where

import ClassyPrelude
import Data.Char
import Safe (atNote)
import System.Random

generateToken :: RandomGen g => g -> Int -> Text
generateToken randomGen numOfChars =
let chars = filter isAlphaNum ['0'..'z'] -- generates a list of eligible chars for the token
-- given a list of 'random' numbers, returns the list of number resulting
-- by the application of: generated number mod number of eligible chars.
-- this ensures that we won't use an invalid index when trying to get
-- a char from the list of eligible chars using the generated numbers.
ints = (\i -> i `mod` length chars) <$> take numOfChars (randoms randomGen :: [Int])
token = pack $ map (atNote "invalid index of chars array" chars) ints
in token
@@ -23,6 +23,7 @@ library
Handler.Common
Handler.Home
Handler.Free
Helpers.Tokens

if flag(dev) || flag(library-only)
cpp-options: -DDEVELOPMENT
@@ -84,6 +85,7 @@ library
, vector
, time
, wai
, random
-- yesod Could not find module 'Foreign.Store'
-- need to find a better way so there is no need
-- to include this in the final binary.
@@ -98,6 +100,24 @@ executable flora
build-depends: base, flora
ghc-options: -W -Wall -Werror -threaded -O -rtsopts -with-rtsopts=-N

extensions: TemplateHaskell
QuasiQuotes
OverloadedStrings
NoImplicitPrelude
CPP
MultiParamTypeClasses
TypeFamilies
GADTs
GeneralizedNewtypeDeriving
FlexibleContexts
FlexibleInstances
EmptyDataDecls
NoMonomorphismRestriction
DeriveDataTypeable
ViewPatterns
TupleSections
RecordWildCards

test-suite test
type: exitcode-stdio-1.0
main-is: Spec.hs
@@ -106,6 +126,7 @@ test-suite test

other-modules: Handler.CommonSpec
, Handler.HomeSpec
, Helpers.TokensSpec
, TestImport

extensions: TemplateHaskell
@@ -139,3 +160,4 @@ test-suite test
, hspec >= 2.0.0
, classy-prelude
, classy-prelude-yesod
, random
@@ -1,7 +1,7 @@
# For more information, see: https://github.com/commercialhaskell/stack/blob/release/doc/yaml_configuration.md

# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
resolver: lts-5.0
resolver: lts-5.6

# Local packages, usually specified by relative directory name
packages:
@@ -0,0 +1,17 @@
module Helpers.TokensSpec (spec) where

import System.Random
import Helpers.Tokens
import TestImport

spec :: Spec
spec = do
describe "Tokens" $ do
it "generates a token of a given length " $ do
g <- newStdGen
g' <- newStdGen

let token1 = generateToken g 10
token2 = generateToken g' 10

token1 == token2 `shouldBe` False

0 comments on commit 35e0d7f

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.