Skip to content

Commit

Permalink
[upd][#44] splitted auth library
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Maggi committed Sep 6, 2016
1 parent 32b8c0e commit c2c2372
Show file tree
Hide file tree
Showing 19 changed files with 209 additions and 185 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ TEST_BENCHMARK ?= no
TOPLEVEL_PKG := github.com/nexocrew/3nigm4
IMPL_LIST := authserver storageservice 3n4cli #<-- Implementation directories
COMMON_LIST := lib/version lib/logo lib/itm lib/logger lib/crypto \
lib/messages lib/client lib/filemanager lib/s3 lib/auth \
lib/messages lib/client lib/filemanager lib/s3 \
lib/auth/client lib/auth/server lib/auth/types \
lib/storageclient

# List building
Expand Down
2 changes: 1 addition & 1 deletion authserver/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// Internal dependencies
import (
"github.com/nexocrew/3nigm4/lib/auth"
"github.com/nexocrew/3nigm4/lib/auth/server"
)

// Third party libs
Expand Down
9 changes: 5 additions & 4 deletions authserver/serve_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (

// Internal dependencies
import (
"github.com/nexocrew/3nigm4/lib/auth"
auth "github.com/nexocrew/3nigm4/lib/auth/server"
db "github.com/nexocrew/3nigm4/lib/database/client"
)

// Third party libs
Expand Down Expand Up @@ -48,13 +49,13 @@ func init() {
// in unit-tests, do not mess with it for other reasons.
// The default, production targeting, implementation uses Mongodb
// as backend database system.
var databaseStartup func(*args) (auth.Database, error) = mgoStartup
var databaseStartup func(*args) (db.Database, error) = mgoStartup

// mgoStartup implement startup logic for a mongodb based database
// connection.
func mgoStartup(arguments *args) (auth.Database, error) {
func mgoStartup(arguments *args) (db.Database, error) {
// startup db
mgodb, err := auth.MgoSession(&auth.DbArgs{
mgodb, err := db.MgoSession(&db.DbArgs{
Addresses: strings.Split(arguments.dbAddresses, ","),
User: arguments.dbUsername,
Password: arguments.dbPassword,
Expand Down
2 changes: 1 addition & 1 deletion authserver/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

// Internal dependencies.
import (
"github.com/nexocrew/3nigm4/lib/auth"
"github.com/nexocrew/3nigm4/lib/auth/server"
"github.com/nexocrew/3nigm4/lib/itm"
"github.com/nexocrew/3nigm4/lib/logger"
wq "github.com/nexocrew/3nigm4/lib/workingqueue"
Expand Down
29 changes: 17 additions & 12 deletions lib/auth/authclient.go → lib/auth/client/authclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
// Author: Guido Ronchetti <[email protected]>
// v1.0 16/06/2016
//
package auth
package authclient

// Std golang packages
import (
"fmt"
"net/rpc"
)

// 3n4 libraries
import (
t "github.com/nexocrew/3nigm4/lib/auth/types"
)

// AuthClient is the interface used to interact
// with authentication services.
type AuthClient interface {
Login(string, string) ([]byte, error) // manage user's login;
Logout([]byte) ([]byte, error) // manage user's logout;
AuthoriseAndGetInfo([]byte) (*UserInfoResponseArg, error) // returns authenticated user infos or an error;
Close() error // closes eventual connections.
Login(string, string) ([]byte, error) // manage user's login;
Logout([]byte) ([]byte, error) // manage user's logout;
AuthoriseAndGetInfo([]byte) (*t.UserInfoResponseArg, error) // returns authenticated user infos or an error;
Close() error // closes eventual connections.
}

// AuthRpc implements the RPC default client for
Expand All @@ -42,8 +47,8 @@ func NewAuthRpc(addr string, port int) (*AuthRpc, error) {
// Login grant access to users, over RPC, using username and password.
func (a *AuthRpc) Login(username string, password string) ([]byte, error) {
// perform login on RPC service
var loginResponse LoginResponseArg
err := a.client.Call("Login.Login", &LoginRequestArg{
var loginResponse t.LoginResponseArg
err := a.client.Call("Login.Login", &t.LoginRequestArg{
Username: username,
Password: password,
}, &loginResponse)
Expand All @@ -55,8 +60,8 @@ func (a *AuthRpc) Login(username string, password string) ([]byte, error) {

// Logout remove actual active sessions over RPC.
func (a *AuthRpc) Logout(token []byte) ([]byte, error) {
var logoutResponse LogoutResponseArg
err := a.client.Call("Login.Logout", &LogoutRequestArg{
var logoutResponse t.LogoutResponseArg
err := a.client.Call("Login.Logout", &t.LogoutRequestArg{
Token: token,
}, &logoutResponse)
if err != nil {
Expand All @@ -67,10 +72,10 @@ func (a *AuthRpc) Logout(token []byte) ([]byte, error) {

// AuthoriseAndGetInfo if the token is valid returns info about
// the associated user over RPC service.
func (a *AuthRpc) AuthoriseAndGetInfo(token []byte) (*UserInfoResponseArg, error) {
func (a *AuthRpc) AuthoriseAndGetInfo(token []byte) (*t.UserInfoResponseArg, error) {
// verify token and retrieve user infos
var authResponse UserInfoResponseArg
err := a.client.Call("SessionAuth.UserInfo", &AuthenticateRequestArg{
var authResponse t.UserInfoResponseArg
err := a.client.Call("SessionAuth.UserInfo", &t.AuthenticateRequestArg{
Token: token,
}, &authResponse)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// optimisation logic.
//

package auth
package authmock

// Std golang libs
import (
Expand Down
10 changes: 7 additions & 3 deletions lib/auth/global.go → lib/auth/server/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@
// db client (that will be copyied by all functions).
//

package auth
package authserver

import (
"sync"
)

import (
db "github.com/nexocrew/3nigm4/lib/database/client"
)

// Global vars protecting mutex.
var mtx sync.Mutex

// Runtime allocated global base database instance.
var dbclient Database
var dbclient db.Database

// SetGlobalDbClient must be called to set the global db client,
// that implements the Database interface, to be used by RPC
// exposed functions. This function must be always invoked before
// proceeding registering other fucntions.
func SetGlobalDbClient(database Database) {
func SetGlobalDbClient(database db.Database) {
mtx.Lock()
dbclient = database
mtx.Unlock()
Expand Down
73 changes: 17 additions & 56 deletions lib/auth/session_model.go → lib/auth/server/session_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// v1.0 16/06/2016
//

package auth
package authserver

// Golang std libs
import (
Expand All @@ -13,27 +13,17 @@ import (
"time"
)

import (
ty "github.com/nexocrew/3nigm4/lib/auth/types"
)

const (
kTimeToLive = 15 // minutes to live for a session between accesses.
)

// SessionAuth RPC required custom type (using int arbitrarely).
type SessionAuth int

// VoidResponseArg empty return value.
type VoidResponseArg struct{}

// AuthenticateRequestArg define the RPC request struct
type AuthenticateRequestArg struct {
Token []byte // the authentication token.
}

// AuthenticateResponseArg the returned auth structure.
type AuthenticateResponseArg struct {
Username string // the session related username;
LastSeenTime time.Time // last connection from the user.
}

// sessionTimeValid verify the time range between last seen
// time and now, if it exceed the session expiration time (15 min)
// it returns true otherwise false.
Expand All @@ -47,7 +37,7 @@ func sessionTimeValid(now, lastSeen *time.Time, timeToLive time.Duration) bool {
// Authenticate RPC exposed functions verify a session token
// and returns the userid to authenticate user required
// operations.
func (s *SessionAuth) Authenticate(args *AuthenticateRequestArg, response *AuthenticateResponseArg) error {
func (s *SessionAuth) Authenticate(args *ty.AuthenticateRequestArg, response *ty.AuthenticateResponseArg) error {
// check for session
if dbclient == nil {
return fmt.Errorf("invalid db session, unable to proceed")
Expand Down Expand Up @@ -87,21 +77,11 @@ func (s *SessionAuth) Authenticate(args *AuthenticateRequestArg, response *Authe
return nil
}

// UserInfoResponseArg the returned authenticated user
// data.
type UserInfoResponseArg struct {
Username string // the session related username;
FullName string // the user full name;
Email string // the user email address;
Permissions *Permissions // user associated permissions;
LastSeen time.Time // last seen info.
}

// UserInfo RPC exposed function verify a session token
// and returns the user associated data (from the User struct).
// Notice that this function will update the "last seen" time
// stamp as the Authenticate do.
func (s *SessionAuth) UserInfo(args *AuthenticateRequestArg, response *UserInfoResponseArg) error {
func (s *SessionAuth) UserInfo(args *ty.AuthenticateRequestArg, response *ty.UserInfoResponseArg) error {
// check for session
if dbclient == nil {
return fmt.Errorf("invalid db session, unable to proceed")
Expand All @@ -115,7 +95,7 @@ func (s *SessionAuth) UserInfo(args *AuthenticateRequestArg, response *UserInfoR
return fmt.Errorf("invalid nil token data")
}

userResponse := AuthenticateResponseArg{}
userResponse := ty.AuthenticateResponseArg{}
err := s.Authenticate(args, &userResponse)
if err != nil {
return err
Expand All @@ -136,23 +116,11 @@ func (s *SessionAuth) UserInfo(args *AuthenticateRequestArg, response *UserInfoR
return nil
}

//
// Superadmin behaviour: the following functions are intended to
// implement administrative tasks like creating or removing users,
// update user's permissions or logout all users.
//

// UpserUserRequestArg request to upsert user data.
type UpserUserRequestArg struct {
Token []byte // the authentication token;
User User // the user record to be updated.
}

// UpsertUser is an RPC exposed function used to add or update a user in
// the authentication database. If the user is not already present it'll
// be added, otherwise it will be updated. Only Super-Admins will be able
// to use this function.
func (s *SessionAuth) UpsertUser(args *UpserUserRequestArg, response *VoidResponseArg) error {
func (s *SessionAuth) UpsertUser(args *ty.UpserUserRequestArg, response *ty.VoidResponseArg) error {
// check for session
if dbclient == nil {
return fmt.Errorf("invalid db session, unable to proceed")
Expand All @@ -166,8 +134,8 @@ func (s *SessionAuth) UpsertUser(args *UpserUserRequestArg, response *VoidRespon
return fmt.Errorf("invalid nil token data")
}

userinfo := UserInfoResponseArg{}
err := s.UserInfo(&AuthenticateRequestArg{
userinfo := ty.UserInfoResponseArg{}
err := s.UserInfo(&ty.AuthenticateRequestArg{
Token: args.Token,
}, &userinfo)
if err != nil {
Expand All @@ -186,16 +154,9 @@ func (s *SessionAuth) UpsertUser(args *UpserUserRequestArg, response *VoidRespon
return nil
}

// RemoveUserRequestArg request for remove an existing
// user.
type RemoveUserRequestArg struct {
Token []byte // the authentication token;
Username string // the user to be removed.
}

// RemoveUser is an RPC exposed function that removes an existing user
// from the authentication db.
func (s *SessionAuth) RemoveUser(args *RemoveUserRequestArg, response *VoidResponseArg) error {
func (s *SessionAuth) RemoveUser(args *ty.RemoveUserRequestArg, response *ty.VoidResponseArg) error {
// check for session
if dbclient == nil {
return fmt.Errorf("invalid db session, unable to proceed")
Expand All @@ -213,8 +174,8 @@ func (s *SessionAuth) RemoveUser(args *RemoveUserRequestArg, response *VoidRespo
return fmt.Errorf("invalid username: unable to process requesto for nil username")
}
// get user infos
userinfo := UserInfoResponseArg{}
err := s.UserInfo(&AuthenticateRequestArg{
userinfo := ty.UserInfoResponseArg{}
err := s.UserInfo(&ty.AuthenticateRequestArg{
Token: args.Token,
}, &userinfo)
if err != nil {
Expand All @@ -235,7 +196,7 @@ func (s *SessionAuth) RemoveUser(args *RemoveUserRequestArg, response *VoidRespo

// KickOutAllSessions is an RPC exposed function that remove all active sessions from
// the authentication database.
func (s *SessionAuth) KickOutAllSessions(args *AuthenticateRequestArg, response *VoidResponseArg) error {
func (s *SessionAuth) KickOutAllSessions(args *ty.AuthenticateRequestArg, response *ty.VoidResponseArg) error {
// check for session
if dbclient == nil {
return fmt.Errorf("invalid db session, unable to proceed")
Expand All @@ -249,8 +210,8 @@ func (s *SessionAuth) KickOutAllSessions(args *AuthenticateRequestArg, response
return fmt.Errorf("invalid nil token data")
}
// get user infos
userinfo := UserInfoResponseArg{}
err := s.UserInfo(&AuthenticateRequestArg{
userinfo := ty.UserInfoResponseArg{}
err := s.UserInfo(&ty.AuthenticateRequestArg{
Token: args.Token,
}, &userinfo)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// v1.0 16/06/2016
//

package auth
package authserver

// Golang std libs
import (
Expand Down
Loading

0 comments on commit c2c2372

Please sign in to comment.