doc.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. 
  2. package auth // import "imuslab.com/arozos/mod/auth"
  3. FUNCTIONS
  4. func Hash(raw string) string
  5. Hash the given raw string into sha512 hash
  6. TYPES
  7. type AuthAgent struct {
  8. //Session related
  9. SessionName string
  10. SessionStore *sessions.CookieStore
  11. Database *db.Database
  12. LoginRedirectionHandler func(http.ResponseWriter, *http.Request)
  13. //Token related
  14. ExpireTime int64 //Set this to 0 to disable token access
  15. //Autologin Related
  16. AllowAutoLogin bool
  17. // Has unexported fields.
  18. }
  19. func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database, allowReg bool, loginRedirectionHandler func(http.ResponseWriter, *http.Request)) *AuthAgent
  20. Constructor
  21. func (a *AuthAgent) CheckAuth(r *http.Request) bool
  22. Check authentication from request header's session value
  23. func (a *AuthAgent) CheckLogin(w http.ResponseWriter, r *http.Request)
  24. Check if the user has logged in, return true / false in JSON
  25. func (a *AuthAgent) ClearTokenStore()
  26. Run a token store scan and remove all expired tokens
  27. func (a *AuthAgent) Close()
  28. Close the authAgent listener
  29. func (a *AuthAgent) CreateUserAccount(newusername string, password string, group []string) error
  30. Create user account
  31. func (a *AuthAgent) ExportUserListAsCSV() string
  32. Export all the users into a csv file. Should only be usable via command line
  33. as a form of db backup. DO NOT EXPOSE THIS TO HTTP SERVER
  34. func (a *AuthAgent) GetTokenOwner(tokenString string) (string, error)
  35. Get the token owner from the given token
  36. func (a *AuthAgent) GetTokensFromUsername(username string) []AutoLoginToken
  37. func (a *AuthAgent) GetUserCounts() int
  38. Get the number of users in the system
  39. func (a *AuthAgent) GetUserName(w http.ResponseWriter, r *http.Request) (string, error)
  40. Get the current session username from request
  41. func (a *AuthAgent) GetUsernameFromToken(token string) (string, error)
  42. func (a *AuthAgent) HandleAutologinTokenLogin(w http.ResponseWriter, r *http.Request)
  43. func (a *AuthAgent) HandleCheckAuth(w http.ResponseWriter, r *http.Request, handler func(http.ResponseWriter, *http.Request))
  44. This function will handle an http request and redirect to the given login
  45. address if not logged in
  46. func (a *AuthAgent) HandleCreateUserAccountsFromCSV(w http.ResponseWriter, r *http.Request)
  47. CreateUserAccountsFromCSV
  48. This function allow mass import of user accounts for organization purpses.
  49. Must be in the format of:{ username, default password, default group }
  50. format. Each user occupied one new line
  51. func (a *AuthAgent) HandleLogin(w http.ResponseWriter, r *http.Request)
  52. Handle login request, require POST username and password
  53. func (a *AuthAgent) HandleLogout(w http.ResponseWriter, r *http.Request)
  54. Handle logout, reply OK after logged out. WILL NOT DO REDIRECTION
  55. func (a *AuthAgent) HandleRegister(w http.ResponseWriter, r *http.Request)
  56. Handle new user register. Require POST username, password, group.
  57. func (a *AuthAgent) HandleUnregister(w http.ResponseWriter, r *http.Request)
  58. Handle de-register of users. Require POST username. THIS FUNCTION WILL NOT
  59. CHECK FOR PERMISSION. PLEASE USE WITH PERMISSION HANDLER
  60. func (a *AuthAgent) HandleUserDeleteByGroup(w http.ResponseWriter, r *http.Request)
  61. HandleUserDeleteByGroup handles user batch delete request by group name Set
  62. exact = true will only delete users which the user is 1. inside the given
  63. group and 2. that group is his / her only group
  64. Require paramter: group, exact
  65. func (a *AuthAgent) ListUsers() []string
  66. List all username within the system
  67. func (a *AuthAgent) LoadAutologinTokenFromDB() error
  68. func (a *AuthAgent) LoginUserByRequest(w http.ResponseWriter, r *http.Request, username string, rememberme bool)
  69. func (a *AuthAgent) Logout(w http.ResponseWriter, r *http.Request) error
  70. func (a *AuthAgent) NewAutologinToken(username string) string
  71. func (a *AuthAgent) NewToken(owner string) string
  72. Generate and return a new token that will be valid for the given time
  73. func (a *AuthAgent) NewTokenFromRequest(w http.ResponseWriter, r *http.Request) (string, error)
  74. Create a new token based on the given HTTP request
  75. func (a *AuthAgent) RegisterPublicAPIs(ep AuthEndpoints)
  76. Register APIs that requires public access
  77. func (a *AuthAgent) RemoveAutologinToken(token string)
  78. func (a *AuthAgent) RemoveAutologinTokenByUsername(username string)
  79. func (a *AuthAgent) TokenValid(tokenString string) bool
  80. validate if the given token is valid
  81. func (a *AuthAgent) UnregisterUser(username string) error
  82. func (a *AuthAgent) UpdateSessionExpireTime(w http.ResponseWriter, r *http.Request) bool
  83. Update the session expire time given the request header.
  84. func (a *AuthAgent) UserExists(username string) bool
  85. Check if the given username exists
  86. func (a *AuthAgent) ValidateUsernameAndPassword(username string, password string) bool
  87. type AuthEndpoints struct {
  88. Login string
  89. Logout string
  90. Register string
  91. CheckLoggedIn string
  92. Autologin string
  93. }
  94. type AutoLoginToken struct {
  95. Owner string
  96. Token string
  97. }
  98. Autologin token. This token will not expire until admin removal