Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Si Le omnisyle

🏠
Working from home
View GitHub Profile
@omnisyle
omnisyle / object_pool_part1.go
Last active December 14, 2019 23:28
Defining Factory function and Object interface
View object_pool_part1.go
type Object interface {
Close()
}
type Factory func() (Object, error)
@omnisyle
omnisyle / middleware.go
Created May 31, 2019 20:35
JWT auth middleware example
View middleware.go
package goliauth
import (
"fmt"
"net/http"
"strings"
)
// AuthenticateJWTMiddleware wraps AuthenticateJWTToken to provide middleware
// this is just an example to show how it can be used as a middleware
@omnisyle
omnisyle / encode_jwt.go
Created May 31, 2019 20:22
Encode things to JWT Token
View encode_jwt.go
package goliauth
import (
"fmt"
jwt "github.com/dgrijalva/jwt-go"
)
// EncodeJWT serialize data into a jwt token using a secret
// This secret must match with the client's secret who's generating the token
@omnisyle
omnisyle / parse_jwt.go
Last active May 31, 2019 19:34
Parse JWT token
View parse_jwt.go
package goliauth
import (
"fmt"
jwt "github.com/dgrijalva/jwt-go"
)
// Claims is an alias for MapClaims
type Claims = jwt.MapClaims
@omnisyle
omnisyle / get.go
Created April 16, 2019 04:51
A function to get an App object given a public key
View get.go
package app
import (
"encoding/hex"
"fmt"
"github.com/omnisyle/goliauth"
)
func GetApp(publicKey, dbURL string) *App {
@omnisyle
omnisyle / create.go
Created April 16, 2019 04:40
Service to create an App object in database
View create.go
package app
import (
"fmt"
"github.com/omnisyle/goliauth"
)
func CreateApp(name, dbURL string) *App {
publicKey := goliauth.NewRandomKey()
@omnisyle
omnisyle / handlers.go
Created April 16, 2019 04:24
Handlers for app command to create and get app
View handlers.go
func createApp(cmd *cobra.Command, args []string) {
appName := args[0]
keyPair := app.CreateApp(appName, db)
printResult(keyPair)
}
func getApp(cmd *cobra.Command, args []string) {
publicKey := args[0]
keyPair := app.GetApp(publicKey, db)
printResult(keyPair)
@omnisyle
omnisyle / key.go
Created April 16, 2019 02:55
Command to generate a random 32 bits key and print to the screen
View key.go
package cmd
import (
"fmt"
"github.com/omnisyle/goliauth"
"github.com/spf13/cobra"
)
func keyCmd() *cobra.Command {
View root.go
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
@omnisyle
omnisyle / encrypt.go
Created April 1, 2019 02:26
Encrypt text using AES256 GCM in golang
View encrypt.go
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"os"
)
// Encrypt will encrypt a raw string to
// an encrypted value