auth

package module
v0.0.0-...-558568e Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2014 License: MPL-2.0 Imports: 12 Imported by: 0

README

In-App Cloud Auth Build Status

An authentication microservice. You can use it directly as a REST API or use the package as part of your Go web application. It is built with Goji and requires PostgreSQL. cmd/auth-server/main.go will show you how to use the package in your own code.

API

It uses JSON API for request and response format.

Creating a user

To create a user, you need the following information:

name type description
email string Required. A valid email
password string Required. A password

Then you can make a request like this:

POST /users
Content-Type: application/json
Accept: application/json

{
  "data": [{
    "email": "john@doe.com",
    "password": "p@ssw0rd"
  }]
}

Endpoint will respond with 400 Bad Request if the body is not JSON and with 422 Unprocessable Entity if the body has invalid formatting or params.

Log in

It's almost the same as user creation, the only change is the URI.

POST /sessions
Content-Type: application/json
Accept: application/json

{
  "data": [{
    "email": "john@doe.com",
    "password": "p@ssw0rd"
  }]
}

Current user

You must set the Authorization header with value Bearer token where token is the value you will get from the other endpoints.

GET /users/me
Accept: application/json
Authorization: Bearer jnshionefv2r3rnvdfoi239

Response for all endpoints

name type description
id int User ID
email string User Email
token string A JWT token
{
  "data": [{
    "id": 1,
    "email": "john@doe.com",
    "token": "jnshionefv2r3rnvdfoi239"
  }]
}

Endpoint will respond with 401 Unauthorized if the token is invalid.

Run

Locally

make setup
make server

If you have a custom environment for PostgreSQL, please update variable DATABASE_URL in file .env. It runs on port 8080 by default but you can add PORT in .env to change the port.

Test

make

If you have a custom environment for PostgreSQL, please update TEST_DB in file Makefile.

FAQ

Is it ready for production?

No. It need more testing, refinements and features before a real release.

Documentation

Index

Constants

View Source
const (
	PasswordBlank        = "password is blank"
	EmailBlank           = "email is blank"
	EmailInvalid         = "email format is invalid"
	EmailExists          = "email already exists"
	EmailNotFound        = "email does not exist"
	EmailPasswordInvalid = "email and/or password is invalid"
	TokenNotFound        = "token does not exist"
)

Variables

This section is empty.

Functions

func Mux

func Mux(db *sql.DB) http.Handler

Types

type User

type User struct {
	Id       int    `json:"id"`
	Email    string `json:"email"`
	Password string `json:"password,omitempty"`
	Token    string `json:"token,omitempty"`
}

func (*User) Authenticate

func (u *User) Authenticate(password string) error

func (*User) EncryptPassword

func (u *User) EncryptPassword() error

func (*User) GenerateToken

func (u *User) GenerateToken()

type UserRepo

type UserRepo struct {
	// contains filtered or unexported fields
}

func (*UserRepo) Create

func (r *UserRepo) Create() error

func (*UserRepo) FetchByEmail

func (r *UserRepo) FetchByEmail(email string) error

func (*UserRepo) Update

func (r *UserRepo) Update() error

func (*UserRepo) Validate

func (r *UserRepo) Validate() error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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