diff --git a/Makefile b/Makefile index 0b5b544..40167fe 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/authserver/database_test.go b/authserver/database_test.go index d704331..84efd45 100644 --- a/authserver/database_test.go +++ b/authserver/database_test.go @@ -19,7 +19,7 @@ import ( // Internal dependencies import ( - "github.com/nexocrew/3nigm4/lib/auth" + "github.com/nexocrew/3nigm4/lib/auth/server" ) // Third party libs diff --git a/authserver/serve_cmd.go b/authserver/serve_cmd.go index 18e23a4..3a26920 100644 --- a/authserver/serve_cmd.go +++ b/authserver/serve_cmd.go @@ -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 @@ -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, diff --git a/authserver/serve_test.go b/authserver/serve_test.go index 9bed2da..490fdf1 100644 --- a/authserver/serve_test.go +++ b/authserver/serve_test.go @@ -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" diff --git a/lib/auth/authclient.go b/lib/auth/client/authclient.go similarity index 65% rename from lib/auth/authclient.go rename to lib/auth/client/authclient.go index 1df3889..37d2cd6 100644 --- a/lib/auth/authclient.go +++ b/lib/auth/client/authclient.go @@ -3,7 +3,7 @@ // Author: Guido Ronchetti // v1.0 16/06/2016 // -package auth +package authclient // Std golang packages import ( @@ -11,13 +11,18 @@ import ( "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 @@ -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) @@ -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 { @@ -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 { diff --git a/lib/auth/authclient_mock.go b/lib/auth/mock/client_mock.go similarity index 99% rename from lib/auth/authclient_mock.go rename to lib/auth/mock/client_mock.go index b1d5eb1..26f4c61 100644 --- a/lib/auth/authclient_mock.go +++ b/lib/auth/mock/client_mock.go @@ -9,7 +9,7 @@ // optimisation logic. // -package auth +package authmock // Std golang libs import ( diff --git a/lib/auth/global.go b/lib/auth/server/global.go similarity index 82% rename from lib/auth/global.go rename to lib/auth/server/global.go index cbb12e0..73d20fc 100644 --- a/lib/auth/global.go +++ b/lib/auth/server/global.go @@ -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() diff --git a/lib/auth/session_model.go b/lib/auth/server/session_model.go similarity index 71% rename from lib/auth/session_model.go rename to lib/auth/server/session_model.go index 9d7dd5c..8ad16d5 100644 --- a/lib/auth/session_model.go +++ b/lib/auth/server/session_model.go @@ -4,7 +4,7 @@ // v1.0 16/06/2016 // -package auth +package authserver // Golang std libs import ( @@ -13,6 +13,10 @@ import ( "time" ) +import ( + ty "github.com/nexocrew/3nigm4/lib/auth/types" +) + const ( kTimeToLive = 15 // minutes to live for a session between accesses. ) @@ -20,20 +24,6 @@ const ( // 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. @@ -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") @@ -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") @@ -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 @@ -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") @@ -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 { @@ -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") @@ -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 { @@ -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") @@ -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 { diff --git a/lib/auth/session_test.go b/lib/auth/server/session_test.go similarity index 99% rename from lib/auth/session_test.go rename to lib/auth/server/session_test.go index 4b11f8c..e62d2f1 100644 --- a/lib/auth/session_test.go +++ b/lib/auth/server/session_test.go @@ -4,7 +4,7 @@ // v1.0 16/06/2016 // -package auth +package authserver // Golang std libs import ( diff --git a/lib/auth/user_model.go b/lib/auth/server/user_model.go similarity index 79% rename from lib/auth/user_model.go rename to lib/auth/server/user_model.go index 8858f97..9468aea 100644 --- a/lib/auth/user_model.go +++ b/lib/auth/server/user_model.go @@ -4,7 +4,7 @@ // v1.0 16/06/2016 // -package auth +package authserver // Golang std libs import ( @@ -16,6 +16,10 @@ import ( "time" ) +import ( + ty "github.com/nexocrew/3nigm4/lib/auth/types" +) + // Third party libs import ( "golang.org/x/crypto/bcrypt" @@ -66,22 +70,10 @@ func generateSessionToken(username string) ([]byte, error) { // Login the RPC required custom type. type Login int -// LoginRequestArg define the RPC request struct -type LoginRequestArg struct { - Username string // the authenticating username; - Password string // plaintext password. -} - -// LoginResponseArg the returned login structure -// having the user assigned session token. -type LoginResponseArg struct { - Token []byte // the session token to be used, from now on, to communicate with server. -} - // Login RPC exposed functions it's create a session token // after verifying that the username and password are already // registered in the system. -func (t *Login) Login(args *LoginRequestArg, response *LoginResponseArg) error { +func (t *Login) Login(args *ty.LoginRequestArg, response *ty.LoginResponseArg) error { // check for session if dbclient == nil { return fmt.Errorf("invalid db session, unable to proceed") @@ -116,7 +108,7 @@ func (t *Login) Login(args *LoginRequestArg, response *LoginResponseArg) error { } // save to the database now := time.Now() - err = client.SetSession(&Session{ + err = client.SetSession(&ty.Session{ Token: token, Username: reference.Username, LoginTime: now, @@ -131,21 +123,9 @@ func (t *Login) Login(args *LoginRequestArg, response *LoginResponseArg) error { return nil } -// LogoutRequestArg is the request passed to logout the -// user's sessions. -type LogoutRequestArg struct { - Token []byte // the session token used to identify the user. -} - -// LogoutResponseArg is the structure used to return the -// list of invalidated sessions. -type LogoutResponseArg struct { - Invalidated []byte -} - // Logout RPC exposed function logout a user, starting from a valid active // session and remove all opened session related to that user. -func (t *Login) Logout(args *LogoutRequestArg, response *LogoutResponseArg) error { +func (t *Login) Logout(args *ty.LogoutRequestArg, response *ty.LogoutResponseArg) error { // check for session if dbclient == nil { return fmt.Errorf("invalid db session, unable to proceed") diff --git a/lib/auth/user_test.go b/lib/auth/server/user_test.go similarity index 99% rename from lib/auth/user_test.go rename to lib/auth/server/user_test.go index 3ffde48..a1ae748 100644 --- a/lib/auth/user_test.go +++ b/lib/auth/server/user_test.go @@ -4,7 +4,7 @@ // v1.0 16/06/2016 // -package auth +package authserver // Golang std libs import ( diff --git a/lib/auth/types.go b/lib/auth/types.go deleted file mode 100644 index ef20015..0000000 --- a/lib/auth/types.go +++ /dev/null @@ -1,51 +0,0 @@ -// -// 3nigm4 auth package -// Author: Guido Ronchetti -// v1.0 16/06/2016 -// - -package auth - -import ( - "time" -) - -// Level type describe available user's permission -// levels. -type Level uint - -// Common levels used to identify tipical figures -// that can access a service, this list can be expanded. -const ( - LevelUser Level = iota // common user, will not be able to administer a service; - LevelAdmin Level = iota // administrator will be able to perform maintainance tasks. -) - -// Permissions struct describe user's permisisons -// on a service basis, if the user is a sper-admin -// a special bool flag will be setted. -type Permissions struct { - SuperAdmin bool `bson:"superadmin,omitempty"` // special user that have all permissions on all services; - Services map[string]Level `bson:"services"` // permissions organised per service, the "all" can be used for generalised behaviour. -} - -// User struct identify a registered -// user to the service. -type User struct { - Username string `bson:"username"` // user name; - FullName string `bson:"fullname,omitempty"` // complete full name; - HashedPassword []byte `bson:"pwdhash"` // hashed password; - Email string `bson:"email,omitempty"` // user's verified email; - Permissions Permissions `bson:"permissions"` // the permissions associated to the user; - IsDisabled bool `bson:"disabled"` // user active (true) or not (false). -} - -// Session contains information about loggedin -// for authenticated users. -type Session struct { - Token []byte `bson:"token"` // token for the session; - Username string `bson:"username"` // username associated to session; - LoginTime time.Time `bson:"login_ts"` // timestamp of login time for this session; - LastSeenTime time.Time `bson:"lastseen_ts"` // last call to an API done by the user; - TimeToLive time.Duration `bson:"timetolive"` // time of validity of the session. -} diff --git a/lib/auth/types/types.go b/lib/auth/types/types.go new file mode 100644 index 0000000..91d30bf --- /dev/null +++ b/lib/auth/types/types.go @@ -0,0 +1,118 @@ +// +// 3nigm4 auth package +// Author: Guido Ronchetti +// v1.0 16/06/2016 +// + +package authserver + +import ( + "time" +) + +// Level type describe available user's permission +// levels. +type Level uint + +// Common levels used to identify tipical figures +// that can access a service, this list can be expanded. +const ( + LevelUser Level = iota // common user, will not be able to administer a service; + LevelAdmin Level = iota // administrator will be able to perform maintainance tasks. +) + +// Permissions struct describe user's permisisons +// on a service basis, if the user is a sper-admin +// a special bool flag will be setted. +type Permissions struct { + SuperAdmin bool `bson:"superadmin,omitempty"` // special user that have all permissions on all services; + Services map[string]Level `bson:"services"` // permissions organised per service, the "all" can be used for generalised behaviour. +} + +// User struct identify a registered +// user to the service. +type User struct { + Username string `bson:"username"` // user name; + FullName string `bson:"fullname,omitempty"` // complete full name; + HashedPassword []byte `bson:"pwdhash"` // hashed password; + Email string `bson:"email,omitempty"` // user's verified email; + Permissions Permissions `bson:"permissions"` // the permissions associated to the user; + IsDisabled bool `bson:"disabled"` // user active (true) or not (false). +} + +// Session contains information about loggedin +// for authenticated users. +type Session struct { + Token []byte `bson:"token"` // token for the session; + Username string `bson:"username"` // username associated to session; + LoginTime time.Time `bson:"login_ts"` // timestamp of login time for this session; + LastSeenTime time.Time `bson:"lastseen_ts"` // last call to an API done by the user; + TimeToLive time.Duration `bson:"timetolive"` // time of validity of the session. +} + +// 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. +} + +// 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. +} + +// +// 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. +} + +// RemoveUserRequestArg request for remove an existing +// user. +type RemoveUserRequestArg struct { + Token []byte // the authentication token; + Username string // the user to be removed. +} + +// LoginRequestArg define the RPC request struct +type LoginRequestArg struct { + Username string // the authenticating username; + Password string // plaintext password. +} + +// LoginResponseArg the returned login structure +// having the user assigned session token. +type LoginResponseArg struct { + Token []byte // the session token to be used, from now on, to communicate with server. +} + +// LogoutRequestArg is the request passed to logout the +// user's sessions. +type LogoutRequestArg struct { + Token []byte // the session token used to identify the user. +} + +// LogoutResponseArg is the structure used to return the +// list of invalidated sessions. +type LogoutResponseArg struct { + Invalidated []byte +} diff --git a/lib/auth/database.go b/lib/database/client/database.go similarity index 88% rename from lib/auth/database.go rename to lib/database/client/database.go index eb47efb..7e61b2d 100644 --- a/lib/auth/database.go +++ b/lib/database/client/database.go @@ -11,7 +11,7 @@ // offline tests. // In production this file is a simple wrapper around // mgo package. -package auth +package database // Golang std libs import ( @@ -20,6 +20,10 @@ import ( "time" ) +import ( + ty "github.com/nexocrew/3nigm4/lib/auth/types" +) + // Third party libs import ( "gopkg.in/mgo.v2" @@ -53,14 +57,14 @@ type Database interface { Copy() Database // retain the db client in a multi-coroutine environment; Close() // release the client; // user behaviour - GetUser(string) (*User, error) // gets a user struct from an argument username; - SetUser(*User) error // creates a new user in the db; - RemoveUser(string) error // remove an user from the db; + GetUser(string) (*ty.User, error) // gets a user struct from an argument username; + SetUser(*ty.User) error // creates a new user in the db; + RemoveUser(string) error // remove an user from the db; // session behaviour - GetSession([]byte) (*Session, error) // search for a session in the db; - SetSession(*Session) error // insert a session in the db; - RemoveSession([]byte) error // remove an existing session; - RemoveAllSessions() error // remove all sessions in the db. + GetSession([]byte) (*ty.Session, error) // search for a session in the db; + SetSession(*ty.Session) error // insert a session in the db; + RemoveSession([]byte) error // remove an existing session; + RemoveAllSessions() error // remove all sessions in the db. } // Mongodb database, wrapping mgo session @@ -136,13 +140,13 @@ func (d *Mongodb) Close() { // GetUser get user strucutre from a given username, if // something wrong returns an error. -func (d *Mongodb) GetUser(username string) (*User, error) { +func (d *Mongodb) GetUser(username string) (*ty.User, error) { // build query selector := bson.M{ "username": bson.M{"$eq": username}, } // perform db query - var user User + var user ty.User err := d.session.DB(d.database).C(d.usersCollection).Find(selector).One(&user) if err != nil { return nil, err @@ -152,7 +156,7 @@ func (d *Mongodb) GetUser(username string) (*User, error) { // SetUser adds an argument User struct to the database, // returns an error if something went wrong. -func (d *Mongodb) SetUser(user *User) error { +func (d *Mongodb) SetUser(user *ty.User) error { selector := bson.M{ "username": user.Username, } @@ -183,13 +187,13 @@ func (d *Mongodb) RemoveUser(username string) error { // GetSession check if a session is available and still valid // veryfing time of last seen contact against pre-defined // timeout value. -func (d *Mongodb) GetSession(token []byte) (*Session, error) { +func (d *Mongodb) GetSession(token []byte) (*ty.Session, error) { // build query selector := bson.M{ "token": bson.M{"$eq": token}, } // perform db query - var session Session + var session ty.Session err := d.session.DB(d.database).C(d.sessionsCollection).Find(selector).One(&session) if err != nil { return nil, err @@ -198,7 +202,7 @@ func (d *Mongodb) GetSession(token []byte) (*Session, error) { } // SetSession add a session data to the database. -func (d *Mongodb) SetSession(session *Session) error { +func (d *Mongodb) SetSession(session *ty.Session) error { selector := bson.M{ "token": session.Token, } diff --git a/lib/auth/database_test.go b/lib/database/mock/database_mock.go similarity index 97% rename from lib/auth/database_test.go rename to lib/database/mock/database_mock.go index 87c642f..353dc98 100644 --- a/lib/auth/database_test.go +++ b/lib/database/mock/database_mock.go @@ -9,7 +9,7 @@ // optimisation logic. // -package auth +package authmock // Golang std libs import ( @@ -27,7 +27,7 @@ type mockdb struct { sessionStorage map[string]*Session } -func newMockDb(args *DbArgs) *mockdb { +func NewMockDb(args *DbArgs) *mockdb { return &mockdb{ addresses: composeDbAddress(args), user: args.User, diff --git a/storageservice/commands.go b/storageservice/commands.go index 6a4453d..ef6b827 100644 --- a/storageservice/commands.go +++ b/storageservice/commands.go @@ -17,14 +17,14 @@ import ( // Internal libs import ( - "github.com/nexocrew/3nigm4/lib/auth" + ty "github.com/nexocrew/3nigm4/lib/auth/types" ct "github.com/nexocrew/3nigm4/lib/commons" ) // createStorageResource upload a data chunk to the S3 backend service // after authorising the user. It operates in async mode to perform the // actual upload using a working queue to integrate S3 backend. -func createStorageResource(w http.ResponseWriter, r *http.Request, args *ct.JobPostRequest, userInfo *auth.UserInfoResponseArg) { +func createStorageResource(w http.ResponseWriter, r *http.Request, args *ct.JobPostRequest, userInfo *ty.UserInfoResponseArg) { if args.Arguments.Data == nil || len(args.Arguments.Data) == 0 { riseError(http.StatusBadRequest, @@ -110,7 +110,7 @@ func createStorageResource(w http.ResponseWriter, r *http.Request, args *ct.JobP // checkAclPermission verify all possible acl scenarios and check if the // requiring user has required permissions to access the file. If user can // download it it'll return true otherwise false. -func checkAclPermission(userInfo *auth.UserInfoResponseArg, fileLog *FileLog) bool { +func checkAclPermission(userInfo *ty.UserInfoResponseArg, fileLog *FileLog) bool { // check access credentials switch fileLog.Acl.Permission { case Private: @@ -138,7 +138,7 @@ func checkAclPermission(userInfo *auth.UserInfoResponseArg, fileLog *FileLog) bo // it is exposed via a REST GET method and returns a txId usable with the verify // API call toretrieve the actual downloaded data (from S3 storage). The user // must be correctly authenticated to be able to access the requested resource. -func retrieveStorageResource(w http.ResponseWriter, r *http.Request, args *ct.JobPostRequest, userInfo *auth.UserInfoResponseArg) { +func retrieveStorageResource(w http.ResponseWriter, r *http.Request, args *ct.JobPostRequest, userInfo *ty.UserInfoResponseArg) { // retain db dbSession := db.Copy() defer dbSession.Close() @@ -200,7 +200,7 @@ func retrieveStorageResource(w http.ResponseWriter, r *http.Request, args *ct.Jo // deleteStorageResource remove a file from the S3 storage: only the original file // owner (who uploaded it) can remove a file from there. -func deleteStorageResource(w http.ResponseWriter, r *http.Request, args *ct.JobPostRequest, userInfo *auth.UserInfoResponseArg) { +func deleteStorageResource(w http.ResponseWriter, r *http.Request, args *ct.JobPostRequest, userInfo *ty.UserInfoResponseArg) { // retain db dbSession := db.Copy() defer dbSession.Close() diff --git a/storageservice/handlers.go b/storageservice/handlers.go index 1ae2e24..231e9dd 100644 --- a/storageservice/handlers.go +++ b/storageservice/handlers.go @@ -18,7 +18,7 @@ import ( // Internal libs import ( - "github.com/nexocrew/3nigm4/lib/auth" + ty "github.com/nexocrew/3nigm4/lib/auth/types" ct "github.com/nexocrew/3nigm4/lib/commons" ) @@ -47,7 +47,7 @@ func riseError(status int, msg string, w http.ResponseWriter, ipa string) { // authoriseGettingUserInfos authorises the provided token // and return user associated data. If returns a nil value // it means something went wrong. -func authoriseGettingUserInfos(authToken string) (*auth.UserInfoResponseArg, error) { +func authoriseGettingUserInfos(authToken string) (*ty.UserInfoResponseArg, error) { if authToken == "" { return nil, fmt.Errorf("authorisation token is nil") } diff --git a/storageservice/serve_cmd.go b/storageservice/serve_cmd.go index 689bbed..4442b3f 100644 --- a/storageservice/serve_cmd.go +++ b/storageservice/serve_cmd.go @@ -15,7 +15,7 @@ import ( // Internal dependencies import ( - "github.com/nexocrew/3nigm4/lib/auth" + auth "github.com/nexocrew/3nigm4/lib/auth/client" s3c "github.com/nexocrew/3nigm4/lib/s3" ) diff --git a/storageservice/serve_test.go b/storageservice/serve_test.go index ece85b7..ad2f14d 100644 --- a/storageservice/serve_test.go +++ b/storageservice/serve_test.go @@ -21,7 +21,8 @@ import ( // Internal dependencies. import ( - "github.com/nexocrew/3nigm4/lib/auth" + "github.com/nexocrew/3nigm4/lib/auth/client" + "github.com/nexocrew/3nigm4/lib/auth/mock" ct "github.com/nexocrew/3nigm4/lib/commons" "github.com/nexocrew/3nigm4/lib/itm" "github.com/nexocrew/3nigm4/lib/logger"