apikey

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2020 License: MIT Imports: 3 Imported by: 0

README

apikey

Simple API key validator middleware for Fiber.

Install

go get -u github.com/fiberweb/apikey

Usage

package main

import (
  "github.com/gofiber/fiber"
  "github.com/fiberweb/apikey"
)

func main() {
  app := fiber.New()
  
  app.Use(apikey.New(apikey.Config{Key: "secret"}))
  app.Get("/", func(c *fiber.Ctx) {
    c.Send("Ok")
  })
  app.Listen("8080")
}

Call the above endpint with ?key=secret in the URL or pass the x-api-key header with value secret you will get success response.

Default configuration

If you don't pass apikey.Config object to the apikey.New() method, it will use default configuration which will require you to specify the api key via environment variable named API_KEY.

$ API_KEY=secret ./MyGoApp
- or -
$ export API_KEY=secret
$ ./MyGoApp

Config

type Config struct {
	// Skip this middleware
	Skip func(*fiber.Ctx) bool
  
	// Key is the api key
	Key string
  
	// ValidatorFunc is the function to validates the request
	ValidatorFunc func(*fiber.Ctx, Config) bool
}
Using Skip

Skip allows you to skip this middleware being executed on certain condition.

app.Use(apikey.New(apikey.Config{
    Skip: func(c *fiber.Ctx) bool {
      // add your logic here
      return true // returning true will skip this middleware.
    }
}))
Using ValidatorFunc

ValidatorFunc allows you to specify your own API key validation logic. Your custom validator function will have access to *fiber.Ctx and Config.

In this example we'll create a simple API key validator where the API key itself need to be passed as part of JSON request body called api_key by the client.

type payload struct {
    ApiKey   string `json:"api_key"`
    Command  string `json:"cmd"`
}

app.Use(apikey.New(apikey.Config{
    Key: "secret",
    ValidatorFunc: func(c *fiber.Ctx, cfg Config) bool {
        var p payload
        
        if err := json.Unmarshal([]byte(c.Body()), &p); err != nil {
            // invalid body
            return false
        }
        return cfg.Key == p.ApiKey
    }
}))
app.Post("/", func(c *fiber.Ctx) {
    c.Send("Success")
})

Now if you create a POST request to that endpoint with {"api_key": "secret", "cmd": "do something"} JSON body you'll get the success response.

Documentation

Index

Constants

View Source
const (
	// DefaultKeyIdentifier is the default api key identifier
	DefaultKeyIdentifier string = "key"
	// DefaultHeaderKeyIdentifier is the default api key identifier in request headers
	DefaultHeaderKeyIdentifier string = "x-api-key"
)

Variables

This section is empty.

Functions

func DefaultValidatorFunc

func DefaultValidatorFunc(c *fiber.Ctx, cfg Config) bool

DefaultValidatorFunc is the default validator function used to validates the request This validator try to look for api key in: - URL's query params - Request headers If api key not found in both of those location, return false

func New

func New(config ...Config) func(*fiber.Ctx)

New returns the middleware function

Types

type Config

type Config struct {
	// Skip this middleware
	Skip func(*fiber.Ctx) bool
	// Key is the api key
	Key string
	// ValidatorFunc is the function to validates the request
	ValidatorFunc func(*fiber.Ctx, Config) bool
}

Config is the configuration object for this middleware

Jump to

Keyboard shortcuts

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