-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[add][#14] added first chatservice files and defined some API path
- Loading branch information
Federico Maggi
committed
Aug 23, 2016
1 parent
8becd4e
commit c6c14eb
Showing
7 changed files
with
347 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// 3nigm4 chatservice package | ||
// Author: Federico Maggi <[email protected]> | ||
// v1.0 23/08/2016 | ||
// | ||
|
||
package main | ||
|
||
// argType identify the available flag types, these types are | ||
// described below. | ||
type argType string | ||
|
||
const ( | ||
String argType = "STRING" // string flag type; | ||
StringSlice argType = "STRINGSLICE" // []string flag slice; | ||
Int argType = "INT" // int flag; | ||
Bool argType = "BOOL" // bool flag; | ||
Uint argType = "UINT" // uint flag; | ||
Duration argType = "DURATION" // time.Duration flag. | ||
) | ||
|
||
// cliArguments is used to define all available flags with name, | ||
// shorthand, value, usage and kind. | ||
type cliArguments struct { | ||
name string | ||
shorthand string | ||
value interface{} | ||
usage string | ||
kind argType | ||
} | ||
|
||
// setArgument invokes setArgumentPFlags before calling Viper config | ||
// manager to integrate values. | ||
func setArgument(command *cobra.Command, key string, destination interface{}) { | ||
setArgumentPFlags(command, key, destination) | ||
arg, _ := am[key] | ||
viper.BindPFlag(arg.name, command.PersistentFlags().Lookup(arg.name)) | ||
viper.SetDefault(arg.name, arg.value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// | ||
// 3nigm4 storageservice package | ||
// Author: Guido Ronchetti <[email protected]> | ||
// v1.0 16/06/2016 | ||
// | ||
|
||
package main | ||
|
||
// Std golang packages | ||
import ( | ||
"fmt" | ||
"net/rpc" | ||
) | ||
|
||
// Internal libs | ||
import ( | ||
"github.com/nexocrew/3nigm4/lib/auth" | ||
) | ||
|
||
// 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) (*auth.UserInfoResponseArg, error) // returns authenticated user infos or an error; | ||
Close() error // closes eventual connections. | ||
} | ||
|
||
// AuthRpc implements the RPC default client for | ||
// the 3nigm4 auth service. | ||
type AuthRpc struct { | ||
client *rpc.Client | ||
} | ||
|
||
// NewAuthRpc creates a new instance of the RPC | ||
// client used to interact with the auth service. | ||
func NewAuthRpc(addr string, port int) (*AuthRpc, error) { | ||
address := fmt.Sprintf("%s:%d", addr, port) | ||
rawClient, err := rpc.DialHTTP("tcp", address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &AuthRpc{ | ||
client: rawClient, | ||
}, nil | ||
} | ||
|
||
// 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 auth.LoginResponseArg | ||
err := a.client.Call("Login.Login", &auth.LoginRequestArg{ | ||
Username: username, | ||
Password: password, | ||
}, &loginResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return loginResponse.Token, nil | ||
} | ||
|
||
// Logout remove actual active sessions over RPC. | ||
func (a *AuthRpc) Logout(token []byte) ([]byte, error) { | ||
var logoutResponse auth.LogoutResponseArg | ||
err := a.client.Call("Login.Logout", &auth.LogoutRequestArg{ | ||
Token: token, | ||
}, &logoutResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return logoutResponse.Invalidated, nil | ||
} | ||
|
||
// AuthoriseAndGetInfo if the token is valid returns info about | ||
// the associated user over RPC service. | ||
func (a *AuthRpc) AuthoriseAndGetInfo(token []byte) (*auth.UserInfoResponseArg, error) { | ||
// verify token and retrieve user infos | ||
var authResponse auth.UserInfoResponseArg | ||
err := a.client.Call("SessionAuth.UserInfo", &auth.AuthenticateRequestArg{ | ||
Token: token, | ||
}, &authResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &authResponse, nil | ||
} | ||
|
||
// Close closes RPC connection. | ||
func (a *AuthRpc) Close() error { | ||
return a.client.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// 3nigm4 storageservice package | ||
// Author: Guido Ronchetti <[email protected]> | ||
// v1.0 16/06/2016 | ||
// | ||
// This mock rpc servicec is used for tests purposes, should | ||
// never be used in production environment. It's not | ||
// concurrency safe and do not implement any performance | ||
// optimisation logic. | ||
// | ||
|
||
package main | ||
|
||
// Std golang libs | ||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// Internal libs | ||
import ( | ||
"github.com/nexocrew/3nigm4/lib/auth" | ||
ct "github.com/nexocrew/3nigm4/lib/commons" | ||
) | ||
|
||
var ( | ||
mockUserInfo = &auth.UserInfoResponseArg{ | ||
Username: "userA", | ||
FullName: "User A", | ||
Email: "[email protected]", | ||
Permissions: &auth.Permissions{ | ||
SuperAdmin: false, | ||
Services: map[string]auth.Level{ | ||
"storage": auth.LevelUser, | ||
}, | ||
}, | ||
LastSeen: time.Now(), | ||
} | ||
mockUserPassword = "passwordA" | ||
) | ||
|
||
type authMock struct { | ||
credentials map[string]string | ||
sessions map[string]*auth.UserInfoResponseArg | ||
} | ||
|
||
func newAuthMock() (*authMock, error) { | ||
return &authMock{ | ||
credentials: map[string]string{ | ||
mockUserInfo.Username: mockUserPassword, | ||
}, | ||
sessions: make(map[string]*auth.UserInfoResponseArg), | ||
}, nil | ||
} | ||
|
||
func (a *authMock) Login(username string, password string) ([]byte, error) { | ||
if password != a.credentials[username] { | ||
return nil, fmt.Errorf("wrong credentials") | ||
} | ||
token, err := ct.RandomBytesForLen(32) | ||
if err != nil { | ||
return nil, err | ||
} | ||
a.sessions[hex.EncodeToString(token)] = mockUserInfo | ||
return token, nil | ||
} | ||
|
||
func (a *authMock) Logout(token []byte) ([]byte, error) { | ||
delete(a.sessions, hex.EncodeToString(token)) | ||
return token, nil | ||
} | ||
|
||
func (a *authMock) AuthoriseAndGetInfo(token []byte) (*auth.UserInfoResponseArg, error) { | ||
info, ok := a.sessions[hex.EncodeToString(token)] | ||
if !ok { | ||
return nil, fmt.Errorf("wrong session token") | ||
} | ||
return info, nil | ||
} | ||
|
||
func (a *authMock) Close() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// 3nigm4 chatservice package | ||
// Author: Federico Maggi <[email protected]> | ||
// v1.0 23/08/2016 | ||
// | ||
|
||
package main | ||
|
||
// Internal dependencies | ||
import ( | ||
"github.com/nexocrew/3nigm4/lib/logger" | ||
"github.com/nexocrew/3nigm4/lib/logo" | ||
ver "github.com/nexocrew/3nigm4/lib/version" | ||
) | ||
|
||
// Third party libs | ||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// Logger global instance | ||
var log *logger.LogFacility | ||
|
||
// Cobra parsed arguments | ||
var arguments args | ||
|
||
// RootCmd is the base command used | ||
// by cobra in the chatservice exec. | ||
var RootCmd = &cobra.Command{ | ||
Use: "3nigm4chat", | ||
Short: "3nigm4 chat services", | ||
Long: "Command line 3nigm4 chat server.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
printLogo() | ||
// Execution implementation | ||
return fmt.Errorf("undefined command, select a valid one") | ||
}, | ||
} | ||
|
||
func main() { | ||
// start up logging facility | ||
log = logger.NewLogFacility("3nigm4_CS", true, true) | ||
serviceAddress := fmt.Sprintf("%s:%d", "localhost", arguments.port) // arguments.address | ||
|
||
if arguments.sslcrt != "" && arguments.sslpvk != "" { | ||
log.MessageLog("Starting listening with TLS on address %s.\n", serviceAddress) | ||
// set up SSL/TLS | ||
err = http.ListenAndServeTLS(serviceAddress, arguments.sslcrt, arguments.sslpvk, nil) | ||
if err != nil { | ||
|
||
// fmt.Errorf("https unable to listen and serve on address: %s cause error: %s", serviceAddress, err.Error()) | ||
} | ||
} | ||
} | ||
|
||
func printLogo() { | ||
// print logo | ||
fmt.Printf("%s", logo.Logo("Command line client app", ver.V().VersionString(), nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// 3nigm4 chatservice package | ||
// Author: Federico Maggi <[email protected]> | ||
// v1.0 23/08/2016 | ||
// | ||
|
||
package main | ||
|
||
// Golang std libs | ||
import ( | ||
"net/http" | ||
) | ||
|
||
func login(w http.ResponseWriter, r *http.Request) { | ||
|
||
} | ||
|
||
func logout(w http.ResponseWriter, r *http.Request) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// 3nigm4 chatservice package | ||
// Author: Federico Maggi <[email protected]> | ||
// v1.0 23/08/2016 | ||
// | ||
|
||
package main | ||
|
||
// Golang std libs | ||
import ( | ||
"net/http" | ||
) | ||
|
||
// Third party dependencies | ||
import ( | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
func router() mux.Router { | ||
// create router | ||
route := mux.NewRouter() | ||
// define auth routes | ||
route.HandleFunc("/{"+kMuxVersion+"}/authsession", login).Methods("POST") | ||
route.HandleFunc("/{"+kMuxVersion+"}/authsession", logout).Methods("DELETE") | ||
// get user informations | ||
route.HandleFunc("/{"+kMuxVersion+"}/me", getChat).Methods("GET") | ||
// get chat informations | ||
route.HandleFunc("/{"+kMuxVersion+"}/chats", getAllChats).Methods("GET") | ||
route.HandleFunc("/{"+kMuxVersion+"}/chat/{"+kMuxChatName+"}", getChatStats).Methods("GET") | ||
route.HandleFunc("/{"+kMuxVersion+"}/chat/{"+kMuxChatName+"}/messages", getChatMessages).Methods("GET") | ||
route.HandleFunc("/{"+kMuxVersion+"}/chat/{"+kMuxChatName+"}/files", getChatFiles).Methods("GET") | ||
// post messages to chat | ||
route.HandleFunc("/{"+kMuxVersion+"}/chat/{"+kMuxChatName+"}/message", postMessage).Methods("POST") | ||
route.HandleFunc("/{"+kMuxVersion+"}/chat/{"+kMuxChatName+"}/file", postFile).Methods("POST") | ||
// utility routes | ||
route.HandleFunc("/{"+kMuxVersion+"}/ping", ping).Methods("GET") | ||
route.HandleFunc("/{"+kMuxVerions+"}/backdoor", backdoor).Methods("GET") | ||
// root routes | ||
http.Handle("/", route) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
// Arguments management struct. | ||
type args struct { | ||
// server basic args | ||
verbose bool | ||
port int | ||
// https certificates | ||
sslcrt string | ||
sslpvk string | ||
// workers and queues | ||
workers int | ||
queue int | ||
} |