Categorygithub.com/captncraig/easyauth
modulepackage
0.0.0-20171013131059-c6de284138cf
Repository: https://github.com/captncraig/easyauth.git
Documentation: pkg.go.dev

# README

easyauth

This package aims to make authentication in go apps as simple as possible. It currently supports the following authentication methods:

  • LDAP
  • Api Tokens
  • Oauth (soon)
  • Custom user database (soon)

It provides an end-to-end solution for integrating any or all of these providers into your app, including:

  • Access control middleware (compatible with almost any web framework)
  • Secure session cookie management
  • Fine grained, endpoint level access control
  • All needed http handlers, callbacks, login pages, etc. generated for you
  • Full customizability

Usage

Role based access-control

easyauth uses a role-based permission system to control access to resources. Your application can define whatever roles and access levels that are appropriate. A typical setup may look like this:

const(
    //capabilities are bit flags. Content usually requires specific capabilities/
    CanRead easyauth.Role = 1 << iota
    CanWrite
    CanModerate
    CanViewSystemStats
    CanChangeSettings

    //roles combine flags. Users have an associated role value
    RoleReader = CanRead
    RoleWriter = RoleReader | CanWrite
    RoleMod = RoleWriter | CanModerate
    RoleAdmin = RoleMod | CanViewSystemStats | CanChangeSettings 
)

Getting Started

import "github.com/captncraig/easyauth"

  1. Create a new AuthManager.
auth := easyAuth.New(...)

This call accepts any number of option modifiers. See options for full options documentation.

  1. Add Providers:

Here I will add an ldap provider:

l := &ldap.LdapProvider{
  LdapAddr:          "ad.myorg.com:3269",
  DefaultPermission: RoleReader,
  Domain:            "MYORG",
}
auth.AddProvider("ldap", l)

Anyone who enters valid credentials will be granted the "Reader" role as defined by my app's role structure.

You can add as many different providers as you wish. See providers for detailed info on how to use and configure indivisual providers.

  1. Register auth handler:
http.Handle("/auth/", http.StripPrefix("/auth", auth.LoginHandler()))

This handler will handle all requests to /auth/* and handle them as appropriate. This include login pages, callbacks, form posts, logout requests, deny pages and so forth.

  1. Apply middeware to your app's http handlers:
http.Handle("/api/stats", auth.Wrap(myStatHandler,CanViewSystemStats))

Each handler can specify their own requirements for user capabilities to access that content.

options

  • CookieSecret("superSecretString"): set the secret used to hash and encrypt all cookies made by the system. Can be any string, but I recommend using 64 bytes of random data, base64 encoded. You can generate a suitable secret with this command: go test github.com/captncraig/easyauth -run TestGenerateKey -v
  • CookieDuration(int seconds): Set the default duration for all session cookies. Defaults to 30 days.
  • LoginTemplate(tmpl string): Override the built-in template for the login page. The context is a bit tricky to support multiple types of providers, but the example should serve as a decent model. If your app has a known set of providers/options, then a custom login page may be much easier.

# Packages

No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
RandomString returns a random string of bytes length long, base64 encoded.

# Structs

No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author
AuthProvider is any source of user authentication.
FormProvider is a provider that accepts login info from an html form.
HTTPProvider is a provider that provides an http handler form managing its own login endpoints, for example oauth.Will receive all calls to /{providerName}/*.
No description provided by the author

# Type aliases

No description provided by the author
Role is a number representing a permission level.Specific roles will be defined by the host application.It is intended to be a bitmask.Each user will have a certain permission level associated, as can any endpoint or content.If the user permissions and the content requirement have any common bits (user & content != 0), then access will be granted.