goauth

package module
v0.0.0-...-0b8acc3 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2016 License: GPL-3.0 Imports: 9 Imported by: 0

README

goauth

Build Status

A simple and lightweight user authentication library for GO. User's passwords are salted and hashed with sha256. The entire database is then encrypted with AES using the salt as a key. Once initialized the entire database is cached, providing fast authentication. Any changes to the database are written to disk on execution.

go get github.com/iamdh4/goauth

Examples

package main

import (
	"fmt"
	"os"

	"github.com/iamdh4/goauth"
)

func main() {
	// Create the db object.
	db := new(goauth.Database)

	// Load the database. [file location], [salt]
	// You can use a string of any length for the salt.
	err := db.LoadDatabase("users.db", "h&$!z4@d8XN8xWIF")
	if err != nil {
		// Failed to load the database
		fmt.Println("Failed to load database. Error:", err.Error())
		os.Exit(1)
	}

	// Adding a user is as simple as passing
	// [username] & [password] to AddUser().
	db.AddUser("user1", "password")

	// Will fail because user already exists
	err = db.AddUser("user1", "password")
	if err != nil {
		// Failed to load the database
		fmt.Println("Failed to add user. Error:", err.Error())
	}

	db.AddUser("user2", "diffpass")

	// Get a list of usernames from the database.
	users := db.GetUserList()
	fmt.Println(users)

	// Check if a user exists in the database.
	// Will return a boolen
	user := "user1"
	if db.UserExists(user) {
		fmt.Println(user, "is in the database")
	} else {
		fmt.Println(user, "is not in the database")
	}

	user = "nonexistant"
	if db.UserExists(user) {
		fmt.Println(user, "is not in the database")
	} else {
		fmt.Println(user, "is not in the database")
	}

	// Authenticate a user. Will return a boolen
	user = "user1"
	pass := "password"
	if db.Authenticate(user, pass) {
		fmt.Println(user, "passed authentication.")
	} else {
		fmt.Println(user, "failed authentication.")
	}

	// Update user in the database
	err = db.UpdateUser("user2", "anewpass")
	if err != nil {
		// Failed remove user.
		fmt.Println("Failed to update user. Error:", err.Error())
	}

	// Remove a user from the database
	err = db.RemoveUser("user2")
	if err != nil {
		// Failed remove user.
		fmt.Println("Failed to add user. Error:", err.Error())
	} else {
		fmt.Println("Removed \"user2\"")
		fmt.Println(db.GetUserList())
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database struct {
	Location string
	Salt     string
	Cache    UsersObject
}

Database Database Object

func (*Database) AddUser

func (db *Database) AddUser(username string, password string) error

AddUser Add new user to database.

func (*Database) Authenticate

func (db *Database) Authenticate(username string, password string) bool

Authenticate Authenticate users against database. Returns boolen

func (*Database) GetUserList

func (db *Database) GetUserList() []string

GetUserList Returns a list of users from the database.

func (*Database) LoadDatabase

func (db *Database) LoadDatabase(location string, salt string) error

LoadDatabase Initialize the database Object.

func (*Database) RemoveUser

func (db *Database) RemoveUser(username string) error

RemoveUser Remove user from database.

func (*Database) UpdateUser

func (db *Database) UpdateUser(username string, password string) error

UpdateUser Update existing user in database.

func (*Database) UserExists

func (db *Database) UserExists(username string) bool

UserExists Check if user exists in database. Returns boolen

type DoError

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

DoError Struct for errors.

func (*DoError) Error

func (e *DoError) Error() string

type UsersObject

type UsersObject struct {
	Users []UsersType
}

UsersObject User Object

type UsersType

type UsersType struct {
	Username string
	Password string
}

UsersType User Type

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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