GithubHelp home page GithubHelp logo

foxhound-systems / hs-obfuscate Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 0.0 12 KB

Library for easy obfuscation and deobfuscation of integer IDs in custom data types

License: BSD 3-Clause "New" or "Revised" License

Haskell 91.85% Nix 6.25% Makefile 1.90%
haskell hashids obfuscation

hs-obfuscate's Introduction

Obfuscate

This library makes it easy to use obfuscation in your application code, allowing you to obscure numeric ids before sending them to a client (such as in a JSON API or other web service). This library is backed by foxhound-systems/hashids-st, which itself is an implementation of the Hashids.org obfuscation interface.

Implementation

The Web.Obfuscate module provides CanObfuscate and CanDeobfuscate typeclasses along with several default instances for basic data types. In addition to manually defining your own instances, Web.Obfuscate.TH will derive a sensible default using deriveObfuscate.

Example Usage

Basic Usage

import Web.Obfuscate
import Hashids

...

hashidsContext :: HashidsContext
hashidsContext = ctx
    where
      (Right ctx) = mkHashidsContext "test-salt-please-ignore" 7 defaultAlphabet

getUser :: Obfuscated UserId -> IO (User)
getUser obfuscatedUserId =
    maybeUserId <- deobfuscate hashidsContext obfuscatedUserId
    case maybeUserId of
        Just userId ->
          fetchUserById userId

        Nothing ->
          throwIO err400

Deriving Obfuscation Instances with Template Haskell

{-# LANGUAGE TemplateHaskell #-}

module ForumResponse
  where

import Web.Obfuscate
import Web.Obfuscate.TH
import qualified Data.Text as T

-- Another module that defines another custom obfuscatable type
import ForumAdministrator

data ForumResponse = ForumResponse
  { frForumId :: ForumId
  , frName :: Text
  , frDescription :: Text
  , frCreator :: ForumAdministrator
  , frAdministrators :: [ForumAdministrator]
  }

$(deriveObfuscate defaultObfuscationOptions ''ForumResponse)

The above Template Haskell code will generate something like the following:

data ObfuscatedForumResponse = ObfuscatedForumResponse
    { obfrForumId :: Obfuscated ForumId
    , obfrName :: Text
    , obfrDescription :: Text
    , obfrCreator :: Obfuscated ForumAdministrator
    , obfrAdministrators :: Obfuscated [ForumAdministrator]
    }

type instance Obfuscated ForumResponse = ObfuscatedForumResponse

instance CanObfuscate ForumResponse where
    obfuscate ctx forumResponse =
        ObfuscatedForumResponse
            { obfrForumId = obfuscate ctx $ frForumId forumResponse
            , obfrName = frName forumResponse
            , obfrDescription = frDescription forumResponse
            , obfrCreator = obfuscate ctx $ frCreator forumResponse
            , obfrAdministrators = obfuscate ctx $ frAdministrators forumResponse
            }

instance CanDeobfuscateForumResponse where
    deobfuscate ctx obfuscatedForumResponse = do
        forumId <- deobfuscate ctx $ obfrForumId obfuscatedForumResponse
        creator <- deobfuscate ctx $ obfrCreator obfuscatedForumResponse
        administrators <- deobfuscate ctx $ obfrAdministrators obfuscatedForumResponse
        pure $ ForumResponse
            { frForumId = forumId
            , frName = obfrName obfuscatedForumResponse
            , frDescription = obfrDescription obfuscatedForumResponse
            , frCreator = creator
            , frAdministrators = administrators
            }

Development

Development of this library is done using Nix. With nix installed, following command to start a ghcid session in an isolated environment and run the reloading tests:

nix-shell --run "make tests-watch"

Run nix-build to perform a full build of the library.

License

See the LICENSE file.

hs-obfuscate's People

Contributors

belevy avatar charukiewicz avatar

Stargazers

Wasif Baig avatar Artur Sak avatar Murat Ak avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.