c3ba

package module
v0.0.0-...-2812096 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2021 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gin-crypt3-basicauth

Basic authorization middleware for Gin that supports crypt(3) hashed passwords.

Installation

go get github.com/osoderholm/gin-crypt3-basicauth

Usage

Import the package as c3ba

import (
    ...
    c3ba "https://github.com/osoderholm/gin-crypt3-basicauth"
    ...
)

Create a map[string]string of allowed users. This implementation is up to you.

Use the middleware

router := gin.Default()
router.Use(c3ba.BasicAuth(usersMap))

Access the authenticated user from gin.Context

authenticatedUser := c.MustGet(c3ba.AuthUserKey)
Example
package main

import (
    "github.com/gin-gonic/gin"
    c3ba "github.com/osoderholm/gin-crypt3-basicauth"
)

func main() {
    // Allowed users
    users := map[string]string{
        "foo": "$1$QKcl8j2L$OMdLsMX.TpVdOJkfErQWe1",
        "bar": "$5$gHMJY2A9Gv$VMV0P/GWZ7TxzkjkR8eo3D5ft/Rq7wk60ouCxGFU321",
    }
    
    r := gin.Default()

    // Use the middleware
    r.Use(c3ba.BasicAuth(users))
    
    r.GET("/", func(c *gin.Context) {
    	user := c.MustGet(c3ba.AuthUserKey)
        c.JSON(200, gin.H{
            "user": user,
        })
    })

    r.Run() 
}
Creating hashes

crypt(3) hashes can be created in different ways. Below are some examples.

# MD5
printf "password" | mkpasswd --stdin --method=md5

# SHA256
printf "password" | mkpasswd --stdin --method=sha-256

# SHA512
printf "password" | mkpasswd --stdin --method=sha-512

Documentation

Index

Constants

View Source
const AuthUserKey = "apiUser"

AuthUserKey is the key with which one can retrieve the authenticated apiUser from gin.Context

Variables

View Source
var ErrorCryptBasicAuthUnsupportedHash = errors.New("unsupported crypt hash")

ErrorCryptBasicAuthUnsupportedHash is thrown if a hash is not supported by the crypt library

Functions

func BasicAuth

func BasicAuth(users map[string]string) gin.HandlerFunc

BasicAuth is a basic authorization middleware for Gin with crypt(3) support. Takes in a map of users and password hashes where each key is a user and the value is the hashed password. Successfully authenticated users username is stored in gin.Context using AuthUserKey.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址