You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

auth.go 42 kB

Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
7 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
7 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
7 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package user
  6. import (
  7. "errors"
  8. "fmt"
  9. "net/http"
  10. "strings"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/auth"
  13. "code.gitea.io/gitea/modules/auth/oauth2"
  14. "code.gitea.io/gitea/modules/base"
  15. "code.gitea.io/gitea/modules/context"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/recaptcha"
  18. "code.gitea.io/gitea/modules/setting"
  19. "code.gitea.io/gitea/modules/util"
  20. "github.com/go-macaron/captcha"
  21. "github.com/markbates/goth"
  22. "github.com/tstranex/u2f"
  23. )
  24. const (
  25. // tplMustChangePassword template for updating a user's password
  26. tplMustChangePassword = "user/auth/change_passwd"
  27. // tplSignIn template for sign in page
  28. tplSignIn base.TplName = "user/auth/signin"
  29. // tplSignUp template path for sign up page
  30. tplSignUp base.TplName = "user/auth/signup"
  31. // TplActivate template path for activate user
  32. TplActivate base.TplName = "user/auth/activate"
  33. tplForgotPassword base.TplName = "user/auth/forgot_passwd"
  34. tplResetPassword base.TplName = "user/auth/reset_passwd"
  35. tplTwofa base.TplName = "user/auth/twofa"
  36. tplTwofaScratch base.TplName = "user/auth/twofa_scratch"
  37. tplLinkAccount base.TplName = "user/auth/link_account"
  38. tplU2F base.TplName = "user/auth/u2f"
  39. )
  40. // AutoSignIn reads cookie and try to auto-login.
  41. func AutoSignIn(ctx *context.Context) (bool, error) {
  42. if !models.HasEngine {
  43. return false, nil
  44. }
  45. uname := ctx.GetCookie(setting.CookieUserName)
  46. if len(uname) == 0 {
  47. return false, nil
  48. }
  49. isSucceed := false
  50. defer func() {
  51. if !isSucceed {
  52. log.Trace("auto-login cookie cleared: %s", uname)
  53. ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  54. ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  55. }
  56. }()
  57. u, err := models.GetUserByName(uname)
  58. if err != nil {
  59. if !models.IsErrUserNotExist(err) {
  60. return false, fmt.Errorf("GetUserByName: %v", err)
  61. }
  62. return false, nil
  63. }
  64. if val, _ := ctx.GetSuperSecureCookie(
  65. base.EncodeMD5(u.Rands+u.Passwd), setting.CookieRememberName); val != u.Name {
  66. return false, nil
  67. }
  68. isSucceed = true
  69. err = ctx.Session.Set("uid", u.ID)
  70. if err != nil {
  71. return false, err
  72. }
  73. err = ctx.Session.Set("uname", u.Name)
  74. if err != nil {
  75. return false, err
  76. }
  77. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  78. return true, nil
  79. }
  80. func checkAutoLogin(ctx *context.Context) bool {
  81. // Check auto-login.
  82. isSucceed, err := AutoSignIn(ctx)
  83. if err != nil {
  84. ctx.ServerError("AutoSignIn", err)
  85. return true
  86. }
  87. redirectTo := ctx.Query("redirect_to")
  88. if len(redirectTo) > 0 {
  89. ctx.SetCookie("redirect_to", redirectTo, 0, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  90. } else {
  91. redirectTo = ctx.GetCookie("redirect_to")
  92. }
  93. if isSucceed {
  94. ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  95. ctx.RedirectToFirst(redirectTo, setting.AppSubURL+string(setting.LandingPageURL))
  96. return true
  97. }
  98. return false
  99. }
  100. // SignIn render sign in page
  101. func SignIn(ctx *context.Context) {
  102. ctx.Data["Title"] = ctx.Tr("sign_in")
  103. // Check auto-login.
  104. if checkAutoLogin(ctx) {
  105. return
  106. }
  107. orderedOAuth2Names, oauth2Providers, err := models.GetActiveOAuth2Providers()
  108. if err != nil {
  109. ctx.ServerError("UserSignIn", err)
  110. return
  111. }
  112. ctx.Data["OrderedOAuth2Names"] = orderedOAuth2Names
  113. ctx.Data["OAuth2Providers"] = oauth2Providers
  114. ctx.Data["Title"] = ctx.Tr("sign_in")
  115. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login"
  116. ctx.Data["PageIsSignIn"] = true
  117. ctx.Data["PageIsLogin"] = true
  118. ctx.HTML(200, tplSignIn)
  119. }
  120. // SignInPost response for sign in request
  121. func SignInPost(ctx *context.Context, form auth.SignInForm) {
  122. ctx.Data["Title"] = ctx.Tr("sign_in")
  123. orderedOAuth2Names, oauth2Providers, err := models.GetActiveOAuth2Providers()
  124. if err != nil {
  125. ctx.ServerError("UserSignIn", err)
  126. return
  127. }
  128. ctx.Data["OrderedOAuth2Names"] = orderedOAuth2Names
  129. ctx.Data["OAuth2Providers"] = oauth2Providers
  130. ctx.Data["Title"] = ctx.Tr("sign_in")
  131. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login"
  132. ctx.Data["PageIsSignIn"] = true
  133. ctx.Data["PageIsLogin"] = true
  134. if ctx.HasError() {
  135. ctx.HTML(200, tplSignIn)
  136. return
  137. }
  138. u, err := models.UserSignIn(form.UserName, form.Password)
  139. if err != nil {
  140. if models.IsErrUserNotExist(err) {
  141. ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplSignIn, &form)
  142. log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr())
  143. } else if models.IsErrEmailAlreadyUsed(err) {
  144. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignIn, &form)
  145. log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr())
  146. } else if models.IsErrUserProhibitLogin(err) {
  147. log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr())
  148. ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
  149. ctx.HTML(200, "user/auth/prohibit_login")
  150. } else if models.IsErrUserInactive(err) {
  151. if setting.Service.RegisterEmailConfirm {
  152. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  153. ctx.HTML(200, TplActivate)
  154. } else {
  155. log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr())
  156. ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
  157. ctx.HTML(200, "user/auth/prohibit_login")
  158. }
  159. } else {
  160. ctx.ServerError("UserSignIn", err)
  161. }
  162. return
  163. }
  164. // If this user is enrolled in 2FA, we can't sign the user in just yet.
  165. // Instead, redirect them to the 2FA authentication page.
  166. _, err = models.GetTwoFactorByUID(u.ID)
  167. if err != nil {
  168. if models.IsErrTwoFactorNotEnrolled(err) {
  169. handleSignIn(ctx, u, form.Remember)
  170. } else {
  171. ctx.ServerError("UserSignIn", err)
  172. }
  173. return
  174. }
  175. // User needs to use 2FA, save data and redirect to 2FA page.
  176. err = ctx.Session.Set("twofaUid", u.ID)
  177. if err != nil {
  178. ctx.ServerError("UserSignIn", err)
  179. return
  180. }
  181. err = ctx.Session.Set("twofaRemember", form.Remember)
  182. if err != nil {
  183. ctx.ServerError("UserSignIn", err)
  184. return
  185. }
  186. regs, err := models.GetU2FRegistrationsByUID(u.ID)
  187. if err == nil && len(regs) > 0 {
  188. ctx.Redirect(setting.AppSubURL + "/user/u2f")
  189. return
  190. }
  191. ctx.Redirect(setting.AppSubURL + "/user/two_factor")
  192. }
  193. // TwoFactor shows the user a two-factor authentication page.
  194. func TwoFactor(ctx *context.Context) {
  195. ctx.Data["Title"] = ctx.Tr("twofa")
  196. // Check auto-login.
  197. if checkAutoLogin(ctx) {
  198. return
  199. }
  200. // Ensure user is in a 2FA session.
  201. if ctx.Session.Get("twofaUid") == nil {
  202. ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
  203. return
  204. }
  205. ctx.HTML(200, tplTwofa)
  206. }
  207. // TwoFactorPost validates a user's two-factor authentication token.
  208. func TwoFactorPost(ctx *context.Context, form auth.TwoFactorAuthForm) {
  209. ctx.Data["Title"] = ctx.Tr("twofa")
  210. // Ensure user is in a 2FA session.
  211. idSess := ctx.Session.Get("twofaUid")
  212. if idSess == nil {
  213. ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
  214. return
  215. }
  216. id := idSess.(int64)
  217. twofa, err := models.GetTwoFactorByUID(id)
  218. if err != nil {
  219. ctx.ServerError("UserSignIn", err)
  220. return
  221. }
  222. // Validate the passcode with the stored TOTP secret.
  223. ok, err := twofa.ValidateTOTP(form.Passcode)
  224. if err != nil {
  225. ctx.ServerError("UserSignIn", err)
  226. return
  227. }
  228. if ok && twofa.LastUsedPasscode != form.Passcode {
  229. remember := ctx.Session.Get("twofaRemember").(bool)
  230. u, err := models.GetUserByID(id)
  231. if err != nil {
  232. ctx.ServerError("UserSignIn", err)
  233. return
  234. }
  235. if ctx.Session.Get("linkAccount") != nil {
  236. gothUser := ctx.Session.Get("linkAccountGothUser")
  237. if gothUser == nil {
  238. ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
  239. return
  240. }
  241. err = models.LinkAccountToUser(u, gothUser.(goth.User))
  242. if err != nil {
  243. ctx.ServerError("UserSignIn", err)
  244. return
  245. }
  246. }
  247. twofa.LastUsedPasscode = form.Passcode
  248. if err = models.UpdateTwoFactor(twofa); err != nil {
  249. ctx.ServerError("UserSignIn", err)
  250. return
  251. }
  252. handleSignIn(ctx, u, remember)
  253. return
  254. }
  255. ctx.RenderWithErr(ctx.Tr("auth.twofa_passcode_incorrect"), tplTwofa, auth.TwoFactorAuthForm{})
  256. }
  257. // TwoFactorScratch shows the scratch code form for two-factor authentication.
  258. func TwoFactorScratch(ctx *context.Context) {
  259. ctx.Data["Title"] = ctx.Tr("twofa_scratch")
  260. // Check auto-login.
  261. if checkAutoLogin(ctx) {
  262. return
  263. }
  264. // Ensure user is in a 2FA session.
  265. if ctx.Session.Get("twofaUid") == nil {
  266. ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
  267. return
  268. }
  269. ctx.HTML(200, tplTwofaScratch)
  270. }
  271. // TwoFactorScratchPost validates and invalidates a user's two-factor scratch token.
  272. func TwoFactorScratchPost(ctx *context.Context, form auth.TwoFactorScratchAuthForm) {
  273. ctx.Data["Title"] = ctx.Tr("twofa_scratch")
  274. // Ensure user is in a 2FA session.
  275. idSess := ctx.Session.Get("twofaUid")
  276. if idSess == nil {
  277. ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
  278. return
  279. }
  280. id := idSess.(int64)
  281. twofa, err := models.GetTwoFactorByUID(id)
  282. if err != nil {
  283. ctx.ServerError("UserSignIn", err)
  284. return
  285. }
  286. // Validate the passcode with the stored TOTP secret.
  287. if twofa.VerifyScratchToken(form.Token) {
  288. // Invalidate the scratch token.
  289. _, err = twofa.GenerateScratchToken()
  290. if err != nil {
  291. ctx.ServerError("UserSignIn", err)
  292. return
  293. }
  294. if err = models.UpdateTwoFactor(twofa); err != nil {
  295. ctx.ServerError("UserSignIn", err)
  296. return
  297. }
  298. remember := ctx.Session.Get("twofaRemember").(bool)
  299. u, err := models.GetUserByID(id)
  300. if err != nil {
  301. ctx.ServerError("UserSignIn", err)
  302. return
  303. }
  304. handleSignInFull(ctx, u, remember, false)
  305. ctx.Flash.Info(ctx.Tr("auth.twofa_scratch_used"))
  306. ctx.Redirect(setting.AppSubURL + "/user/settings/security")
  307. return
  308. }
  309. ctx.RenderWithErr(ctx.Tr("auth.twofa_scratch_token_incorrect"), tplTwofaScratch, auth.TwoFactorScratchAuthForm{})
  310. }
  311. // U2F shows the U2F login page
  312. func U2F(ctx *context.Context) {
  313. ctx.Data["Title"] = ctx.Tr("twofa")
  314. ctx.Data["RequireU2F"] = true
  315. // Check auto-login.
  316. if checkAutoLogin(ctx) {
  317. return
  318. }
  319. // Ensure user is in a 2FA session.
  320. if ctx.Session.Get("twofaUid") == nil {
  321. ctx.ServerError("UserSignIn", errors.New("not in U2F session"))
  322. return
  323. }
  324. ctx.HTML(200, tplU2F)
  325. }
  326. // U2FChallenge submits a sign challenge to the browser
  327. func U2FChallenge(ctx *context.Context) {
  328. // Ensure user is in a U2F session.
  329. idSess := ctx.Session.Get("twofaUid")
  330. if idSess == nil {
  331. ctx.ServerError("UserSignIn", errors.New("not in U2F session"))
  332. return
  333. }
  334. id := idSess.(int64)
  335. regs, err := models.GetU2FRegistrationsByUID(id)
  336. if err != nil {
  337. ctx.ServerError("UserSignIn", err)
  338. return
  339. }
  340. if len(regs) == 0 {
  341. ctx.ServerError("UserSignIn", errors.New("no device registered"))
  342. return
  343. }
  344. challenge, err := u2f.NewChallenge(setting.U2F.AppID, setting.U2F.TrustedFacets)
  345. if err != nil {
  346. ctx.ServerError("u2f.NewChallenge", err)
  347. return
  348. }
  349. if err = ctx.Session.Set("u2fChallenge", challenge); err != nil {
  350. ctx.ServerError("UserSignIn", err)
  351. return
  352. }
  353. ctx.JSON(200, challenge.SignRequest(regs.ToRegistrations()))
  354. }
  355. // U2FSign authenticates the user by signResp
  356. func U2FSign(ctx *context.Context, signResp u2f.SignResponse) {
  357. challSess := ctx.Session.Get("u2fChallenge")
  358. idSess := ctx.Session.Get("twofaUid")
  359. if challSess == nil || idSess == nil {
  360. ctx.ServerError("UserSignIn", errors.New("not in U2F session"))
  361. return
  362. }
  363. challenge := challSess.(*u2f.Challenge)
  364. id := idSess.(int64)
  365. regs, err := models.GetU2FRegistrationsByUID(id)
  366. if err != nil {
  367. ctx.ServerError("UserSignIn", err)
  368. return
  369. }
  370. for _, reg := range regs {
  371. r, err := reg.Parse()
  372. if err != nil {
  373. log.Fatal("parsing u2f registration: %v", err)
  374. continue
  375. }
  376. newCounter, authErr := r.Authenticate(signResp, *challenge, reg.Counter)
  377. if authErr == nil {
  378. reg.Counter = newCounter
  379. user, err := models.GetUserByID(id)
  380. if err != nil {
  381. ctx.ServerError("UserSignIn", err)
  382. return
  383. }
  384. remember := ctx.Session.Get("twofaRemember").(bool)
  385. if err := reg.UpdateCounter(); err != nil {
  386. ctx.ServerError("UserSignIn", err)
  387. return
  388. }
  389. if ctx.Session.Get("linkAccount") != nil {
  390. gothUser := ctx.Session.Get("linkAccountGothUser")
  391. if gothUser == nil {
  392. ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
  393. return
  394. }
  395. err = models.LinkAccountToUser(user, gothUser.(goth.User))
  396. if err != nil {
  397. ctx.ServerError("UserSignIn", err)
  398. return
  399. }
  400. }
  401. redirect := handleSignInFull(ctx, user, remember, false)
  402. if redirect == "" {
  403. redirect = setting.AppSubURL + "/"
  404. }
  405. ctx.PlainText(200, []byte(redirect))
  406. return
  407. }
  408. }
  409. ctx.Error(401)
  410. }
  411. // This handles the final part of the sign-in process of the user.
  412. func handleSignIn(ctx *context.Context, u *models.User, remember bool) {
  413. handleSignInFull(ctx, u, remember, true)
  414. }
  415. func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyRedirect bool) string {
  416. if remember {
  417. days := 86400 * setting.LogInRememberDays
  418. ctx.SetCookie(setting.CookieUserName, u.Name, days, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  419. ctx.SetSuperSecureCookie(base.EncodeMD5(u.Rands+u.Passwd),
  420. setting.CookieRememberName, u.Name, days, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  421. }
  422. _ = ctx.Session.Delete("openid_verified_uri")
  423. _ = ctx.Session.Delete("openid_signin_remember")
  424. _ = ctx.Session.Delete("openid_determined_email")
  425. _ = ctx.Session.Delete("openid_determined_username")
  426. _ = ctx.Session.Delete("twofaUid")
  427. _ = ctx.Session.Delete("twofaRemember")
  428. _ = ctx.Session.Delete("u2fChallenge")
  429. _ = ctx.Session.Delete("linkAccount")
  430. err := ctx.Session.Set("uid", u.ID)
  431. if err != nil {
  432. log.Error(fmt.Sprintf("Error setting session: %v", err))
  433. }
  434. err = ctx.Session.Set("uname", u.Name)
  435. if err != nil {
  436. log.Error(fmt.Sprintf("Error setting session: %v", err))
  437. }
  438. // Language setting of the user overwrites the one previously set
  439. // If the user does not have a locale set, we save the current one.
  440. if len(u.Language) == 0 {
  441. u.Language = ctx.Locale.Language()
  442. if err := models.UpdateUserCols(u, "language"); err != nil {
  443. log.Error(fmt.Sprintf("Error updating user language [user: %d, locale: %s]", u.ID, u.Language))
  444. return setting.AppSubURL + "/"
  445. }
  446. }
  447. ctx.SetCookie("lang", u.Language, nil, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  448. // Clear whatever CSRF has right now, force to generate a new one
  449. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  450. // Register last login
  451. u.SetLastLogin()
  452. if err := models.UpdateUserCols(u, "last_login_unix"); err != nil {
  453. ctx.ServerError("UpdateUserCols", err)
  454. return setting.AppSubURL + "/"
  455. }
  456. if redirectTo := ctx.GetCookie("redirect_to"); len(redirectTo) > 0 && !util.IsExternalURL(redirectTo) {
  457. ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  458. if obeyRedirect {
  459. ctx.RedirectToFirst(redirectTo)
  460. }
  461. return redirectTo
  462. }
  463. if obeyRedirect {
  464. ctx.Redirect(setting.AppSubURL + "/")
  465. }
  466. return setting.AppSubURL + "/"
  467. }
  468. // SignInOAuth handles the OAuth2 login buttons
  469. func SignInOAuth(ctx *context.Context) {
  470. provider := ctx.Params(":provider")
  471. loginSource, err := models.GetActiveOAuth2LoginSourceByName(provider)
  472. if err != nil {
  473. ctx.ServerError("SignIn", err)
  474. return
  475. }
  476. // try to do a direct callback flow, so we don't authenticate the user again but use the valid accesstoken to get the user
  477. user, gothUser, err := oAuth2UserLoginCallback(loginSource, ctx.Req.Request, ctx.Resp)
  478. if err == nil && user != nil {
  479. // we got the user without going through the whole OAuth2 authentication flow again
  480. handleOAuth2SignIn(user, gothUser, ctx, err)
  481. return
  482. }
  483. err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp)
  484. if err != nil {
  485. ctx.ServerError("SignIn", err)
  486. }
  487. // redirect is done in oauth2.Auth
  488. }
  489. // SignInOAuthCallback handles the callback from the given provider
  490. func SignInOAuthCallback(ctx *context.Context) {
  491. provider := ctx.Params(":provider")
  492. // first look if the provider is still active
  493. loginSource, err := models.GetActiveOAuth2LoginSourceByName(provider)
  494. if err != nil {
  495. ctx.ServerError("SignIn", err)
  496. return
  497. }
  498. if loginSource == nil {
  499. ctx.ServerError("SignIn", errors.New("No valid provider found, check configured callback url in provider"))
  500. return
  501. }
  502. u, gothUser, err := oAuth2UserLoginCallback(loginSource, ctx.Req.Request, ctx.Resp)
  503. handleOAuth2SignIn(u, gothUser, ctx, err)
  504. }
  505. func handleOAuth2SignIn(u *models.User, gothUser goth.User, ctx *context.Context, err error) {
  506. if err != nil {
  507. ctx.ServerError("UserSignIn", err)
  508. return
  509. }
  510. if u == nil {
  511. // no existing user is found, request attach or new account
  512. err = ctx.Session.Set("linkAccountGothUser", gothUser)
  513. if err != nil {
  514. log.Error(fmt.Sprintf("Error setting session: %v", err))
  515. }
  516. ctx.Redirect(setting.AppSubURL + "/user/link_account")
  517. return
  518. }
  519. // If this user is enrolled in 2FA, we can't sign the user in just yet.
  520. // Instead, redirect them to the 2FA authentication page.
  521. _, err = models.GetTwoFactorByUID(u.ID)
  522. if err != nil {
  523. if models.IsErrTwoFactorNotEnrolled(err) {
  524. err = ctx.Session.Set("uid", u.ID)
  525. if err != nil {
  526. log.Error(fmt.Sprintf("Error setting session: %v", err))
  527. }
  528. err = ctx.Session.Set("uname", u.Name)
  529. if err != nil {
  530. log.Error(fmt.Sprintf("Error setting session: %v", err))
  531. }
  532. // Clear whatever CSRF has right now, force to generate a new one
  533. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  534. // Register last login
  535. u.SetLastLogin()
  536. if err := models.UpdateUserCols(u, "last_login_unix"); err != nil {
  537. ctx.ServerError("UpdateUserCols", err)
  538. return
  539. }
  540. if redirectTo := ctx.GetCookie("redirect_to"); len(redirectTo) > 0 {
  541. ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  542. ctx.RedirectToFirst(redirectTo)
  543. return
  544. }
  545. ctx.Redirect(setting.AppSubURL + "/")
  546. } else {
  547. ctx.ServerError("UserSignIn", err)
  548. }
  549. return
  550. }
  551. // User needs to use 2FA, save data and redirect to 2FA page.
  552. err = ctx.Session.Set("twofaUid", u.ID)
  553. if err != nil {
  554. log.Error(fmt.Sprintf("Error setting session: %v", err))
  555. }
  556. err = ctx.Session.Set("twofaRemember", false)
  557. if err != nil {
  558. log.Error(fmt.Sprintf("Error setting session: %v", err))
  559. }
  560. // If U2F is enrolled -> Redirect to U2F instead
  561. regs, err := models.GetU2FRegistrationsByUID(u.ID)
  562. if err == nil && len(regs) > 0 {
  563. ctx.Redirect(setting.AppSubURL + "/user/u2f")
  564. return
  565. }
  566. ctx.Redirect(setting.AppSubURL + "/user/two_factor")
  567. }
  568. // OAuth2UserLoginCallback attempts to handle the callback from the OAuth2 provider and if successful
  569. // login the user
  570. func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Request, response http.ResponseWriter) (*models.User, goth.User, error) {
  571. gothUser, err := oauth2.ProviderCallback(loginSource.Name, request, response)
  572. if err != nil {
  573. return nil, goth.User{}, err
  574. }
  575. user := &models.User{
  576. LoginName: gothUser.UserID,
  577. LoginType: models.LoginOAuth2,
  578. LoginSource: loginSource.ID,
  579. }
  580. hasUser, err := models.GetUser(user)
  581. if err != nil {
  582. return nil, goth.User{}, err
  583. }
  584. if hasUser {
  585. return user, goth.User{}, nil
  586. }
  587. // search in external linked users
  588. externalLoginUser := &models.ExternalLoginUser{
  589. ExternalID: gothUser.UserID,
  590. LoginSourceID: loginSource.ID,
  591. }
  592. hasUser, err = models.GetExternalLogin(externalLoginUser)
  593. if err != nil {
  594. return nil, goth.User{}, err
  595. }
  596. if hasUser {
  597. user, err = models.GetUserByID(externalLoginUser.UserID)
  598. return user, goth.User{}, err
  599. }
  600. // no user found to login
  601. return nil, gothUser, nil
  602. }
  603. // LinkAccount shows the page where the user can decide to login or create a new account
  604. func LinkAccount(ctx *context.Context) {
  605. ctx.Data["Title"] = ctx.Tr("link_account")
  606. ctx.Data["LinkAccountMode"] = true
  607. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  608. ctx.Data["CaptchaType"] = setting.Service.CaptchaType
  609. ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
  610. ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
  611. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  612. ctx.Data["ShowRegistrationButton"] = false
  613. // use this to set the right link into the signIn and signUp templates in the link_account template
  614. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
  615. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
  616. gothUser := ctx.Session.Get("linkAccountGothUser")
  617. if gothUser == nil {
  618. ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
  619. return
  620. }
  621. uname := gothUser.(goth.User).NickName
  622. email := gothUser.(goth.User).Email
  623. ctx.Data["user_name"] = uname
  624. ctx.Data["email"] = email
  625. if len(email) != 0 {
  626. u, err := models.GetUserByEmail(email)
  627. if err != nil && !models.IsErrUserNotExist(err) {
  628. ctx.ServerError("UserSignIn", err)
  629. return
  630. }
  631. if u != nil {
  632. ctx.Data["user_exists"] = true
  633. }
  634. } else if len(uname) != 0 {
  635. u, err := models.GetUserByName(uname)
  636. if err != nil && !models.IsErrUserNotExist(err) {
  637. ctx.ServerError("UserSignIn", err)
  638. return
  639. }
  640. if u != nil {
  641. ctx.Data["user_exists"] = true
  642. }
  643. }
  644. ctx.HTML(200, tplLinkAccount)
  645. }
  646. // LinkAccountPostSignIn handle the coupling of external account with another account using signIn
  647. func LinkAccountPostSignIn(ctx *context.Context, signInForm auth.SignInForm) {
  648. ctx.Data["Title"] = ctx.Tr("link_account")
  649. ctx.Data["LinkAccountMode"] = true
  650. ctx.Data["LinkAccountModeSignIn"] = true
  651. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  652. ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
  653. ctx.Data["CaptchaType"] = setting.Service.CaptchaType
  654. ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
  655. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  656. ctx.Data["ShowRegistrationButton"] = false
  657. // use this to set the right link into the signIn and signUp templates in the link_account template
  658. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
  659. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
  660. gothUser := ctx.Session.Get("linkAccountGothUser")
  661. if gothUser == nil {
  662. ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
  663. return
  664. }
  665. if ctx.HasError() {
  666. ctx.HTML(200, tplLinkAccount)
  667. return
  668. }
  669. u, err := models.UserSignIn(signInForm.UserName, signInForm.Password)
  670. if err != nil {
  671. if models.IsErrUserNotExist(err) {
  672. ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplLinkAccount, &signInForm)
  673. } else {
  674. ctx.ServerError("UserLinkAccount", err)
  675. }
  676. return
  677. }
  678. // If this user is enrolled in 2FA, we can't sign the user in just yet.
  679. // Instead, redirect them to the 2FA authentication page.
  680. _, err = models.GetTwoFactorByUID(u.ID)
  681. if err != nil {
  682. if models.IsErrTwoFactorNotEnrolled(err) {
  683. err = models.LinkAccountToUser(u, gothUser.(goth.User))
  684. if err != nil {
  685. ctx.ServerError("UserLinkAccount", err)
  686. } else {
  687. handleSignIn(ctx, u, signInForm.Remember)
  688. }
  689. } else {
  690. ctx.ServerError("UserLinkAccount", err)
  691. }
  692. return
  693. }
  694. // User needs to use 2FA, save data and redirect to 2FA page.
  695. err = ctx.Session.Set("twofaUid", u.ID)
  696. if err != nil {
  697. log.Error(fmt.Sprintf("Error setting session: %v", err))
  698. }
  699. err = ctx.Session.Set("twofaRemember", signInForm.Remember)
  700. if err != nil {
  701. log.Error(fmt.Sprintf("Error setting session: %v", err))
  702. }
  703. err = ctx.Session.Set("linkAccount", true)
  704. if err != nil {
  705. log.Error(fmt.Sprintf("Error setting session: %v", err))
  706. }
  707. // If U2F is enrolled -> Redirect to U2F instead
  708. regs, err := models.GetU2FRegistrationsByUID(u.ID)
  709. if err == nil && len(regs) > 0 {
  710. ctx.Redirect(setting.AppSubURL + "/user/u2f")
  711. return
  712. }
  713. ctx.Redirect(setting.AppSubURL + "/user/two_factor")
  714. }
  715. // LinkAccountPostRegister handle the creation of a new account for an external account using signUp
  716. func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
  717. ctx.Data["Title"] = ctx.Tr("link_account")
  718. ctx.Data["LinkAccountMode"] = true
  719. ctx.Data["LinkAccountModeRegister"] = true
  720. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  721. ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
  722. ctx.Data["CaptchaType"] = setting.Service.CaptchaType
  723. ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
  724. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  725. ctx.Data["ShowRegistrationButton"] = false
  726. // use this to set the right link into the signIn and signUp templates in the link_account template
  727. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
  728. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
  729. gothUser := ctx.Session.Get("linkAccountGothUser")
  730. if gothUser == nil {
  731. ctx.ServerError("UserSignUp", errors.New("not in LinkAccount session"))
  732. return
  733. }
  734. if ctx.HasError() {
  735. ctx.HTML(200, tplLinkAccount)
  736. return
  737. }
  738. if setting.Service.DisableRegistration {
  739. ctx.Error(403)
  740. return
  741. }
  742. if setting.Service.EnableCaptcha && setting.Service.CaptchaType == setting.ImageCaptcha && !cpt.VerifyReq(ctx.Req) {
  743. ctx.Data["Err_Captcha"] = true
  744. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplLinkAccount, &form)
  745. return
  746. }
  747. if setting.Service.EnableCaptcha && setting.Service.CaptchaType == setting.ReCaptcha {
  748. valid, _ := recaptcha.Verify(form.GRecaptchaResponse)
  749. if !valid {
  750. ctx.Data["Err_Captcha"] = true
  751. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplLinkAccount, &form)
  752. return
  753. }
  754. }
  755. if (len(strings.TrimSpace(form.Password)) > 0 || len(strings.TrimSpace(form.Retype)) > 0) && form.Password != form.Retype {
  756. ctx.Data["Err_Password"] = true
  757. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplLinkAccount, &form)
  758. return
  759. }
  760. if len(strings.TrimSpace(form.Password)) > 0 && len(form.Password) < setting.MinPasswordLength {
  761. ctx.Data["Err_Password"] = true
  762. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplLinkAccount, &form)
  763. return
  764. }
  765. loginSource, err := models.GetActiveOAuth2LoginSourceByName(gothUser.(goth.User).Provider)
  766. if err != nil {
  767. ctx.ServerError("CreateUser", err)
  768. }
  769. u := &models.User{
  770. Name: form.UserName,
  771. Email: form.Email,
  772. Passwd: form.Password,
  773. IsActive: !setting.Service.RegisterEmailConfirm,
  774. LoginType: models.LoginOAuth2,
  775. LoginSource: loginSource.ID,
  776. LoginName: gothUser.(goth.User).UserID,
  777. }
  778. if err := models.CreateUser(u); err != nil {
  779. switch {
  780. case models.IsErrUserAlreadyExist(err):
  781. ctx.Data["Err_UserName"] = true
  782. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplLinkAccount, &form)
  783. case models.IsErrEmailAlreadyUsed(err):
  784. ctx.Data["Err_Email"] = true
  785. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplLinkAccount, &form)
  786. case models.IsErrNameReserved(err):
  787. ctx.Data["Err_UserName"] = true
  788. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplLinkAccount, &form)
  789. case models.IsErrNamePatternNotAllowed(err):
  790. ctx.Data["Err_UserName"] = true
  791. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplLinkAccount, &form)
  792. default:
  793. ctx.ServerError("CreateUser", err)
  794. }
  795. return
  796. }
  797. log.Trace("Account created: %s", u.Name)
  798. // Auto-set admin for the only user.
  799. if models.CountUsers() == 1 {
  800. u.IsAdmin = true
  801. u.IsActive = true
  802. u.SetLastLogin()
  803. if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
  804. ctx.ServerError("UpdateUser", err)
  805. return
  806. }
  807. }
  808. // Send confirmation email
  809. if setting.Service.RegisterEmailConfirm && u.ID > 1 {
  810. models.SendActivateAccountMail(ctx.Context, u)
  811. ctx.Data["IsSendRegisterMail"] = true
  812. ctx.Data["Email"] = u.Email
  813. ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  814. ctx.HTML(200, TplActivate)
  815. if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  816. log.Error("Set cache(MailResendLimit) fail: %v", err)
  817. }
  818. return
  819. }
  820. ctx.Redirect(setting.AppSubURL + "/user/login")
  821. }
  822. func handleSignOut(ctx *context.Context) {
  823. _ = ctx.Session.Delete("uid")
  824. _ = ctx.Session.Delete("uname")
  825. _ = ctx.Session.Delete("socialId")
  826. _ = ctx.Session.Delete("socialName")
  827. _ = ctx.Session.Delete("socialEmail")
  828. ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  829. ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  830. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  831. ctx.SetCookie("lang", "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true) // Setting the lang cookie will trigger the middleware to reset the language ot previous state.
  832. }
  833. // SignOut sign out from login status
  834. func SignOut(ctx *context.Context) {
  835. handleSignOut(ctx)
  836. ctx.Redirect(setting.AppSubURL + "/")
  837. }
  838. // SignUp render the register page
  839. func SignUp(ctx *context.Context) {
  840. ctx.Data["Title"] = ctx.Tr("sign_up")
  841. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
  842. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  843. ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
  844. ctx.Data["CaptchaType"] = setting.Service.CaptchaType
  845. ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
  846. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  847. ctx.HTML(200, tplSignUp)
  848. }
  849. // SignUpPost response for sign up information submission
  850. func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
  851. ctx.Data["Title"] = ctx.Tr("sign_up")
  852. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
  853. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  854. ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
  855. ctx.Data["CaptchaType"] = setting.Service.CaptchaType
  856. ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
  857. //Permission denied if DisableRegistration or AllowOnlyExternalRegistration options are true
  858. if !setting.Service.ShowRegistrationButton {
  859. ctx.Error(403)
  860. return
  861. }
  862. if ctx.HasError() {
  863. ctx.HTML(200, tplSignUp)
  864. return
  865. }
  866. if setting.Service.EnableCaptcha && setting.Service.CaptchaType == setting.ImageCaptcha && !cpt.VerifyReq(ctx.Req) {
  867. ctx.Data["Err_Captcha"] = true
  868. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplSignUp, &form)
  869. return
  870. }
  871. if setting.Service.EnableCaptcha && setting.Service.CaptchaType == setting.ReCaptcha {
  872. valid, _ := recaptcha.Verify(form.GRecaptchaResponse)
  873. if !valid {
  874. ctx.Data["Err_Captcha"] = true
  875. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplSignUp, &form)
  876. return
  877. }
  878. }
  879. if !form.IsEmailDomainWhitelisted() {
  880. ctx.RenderWithErr(ctx.Tr("auth.email_domain_blacklisted"), tplSignUp, &form)
  881. return
  882. }
  883. if form.Password != form.Retype {
  884. ctx.Data["Err_Password"] = true
  885. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplSignUp, &form)
  886. return
  887. }
  888. if len(form.Password) < setting.MinPasswordLength {
  889. ctx.Data["Err_Password"] = true
  890. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplSignUp, &form)
  891. return
  892. }
  893. u := &models.User{
  894. Name: form.UserName,
  895. Email: form.Email,
  896. Passwd: form.Password,
  897. IsActive: !setting.Service.RegisterEmailConfirm,
  898. }
  899. if err := models.CreateUser(u); err != nil {
  900. switch {
  901. case models.IsErrUserAlreadyExist(err):
  902. ctx.Data["Err_UserName"] = true
  903. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplSignUp, &form)
  904. case models.IsErrEmailAlreadyUsed(err):
  905. ctx.Data["Err_Email"] = true
  906. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignUp, &form)
  907. case models.IsErrNameReserved(err):
  908. ctx.Data["Err_UserName"] = true
  909. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplSignUp, &form)
  910. case models.IsErrNamePatternNotAllowed(err):
  911. ctx.Data["Err_UserName"] = true
  912. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplSignUp, &form)
  913. default:
  914. ctx.ServerError("CreateUser", err)
  915. }
  916. return
  917. }
  918. log.Trace("Account created: %s", u.Name)
  919. // Auto-set admin for the only user.
  920. if models.CountUsers() == 1 {
  921. u.IsAdmin = true
  922. u.IsActive = true
  923. u.SetLastLogin()
  924. if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
  925. ctx.ServerError("UpdateUser", err)
  926. return
  927. }
  928. }
  929. // Send confirmation email, no need for social account.
  930. if setting.Service.RegisterEmailConfirm && u.ID > 1 {
  931. models.SendActivateAccountMail(ctx.Context, u)
  932. ctx.Data["IsSendRegisterMail"] = true
  933. ctx.Data["Email"] = u.Email
  934. ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  935. ctx.HTML(200, TplActivate)
  936. if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  937. log.Error("Set cache(MailResendLimit) fail: %v", err)
  938. }
  939. return
  940. }
  941. ctx.Flash.Success(ctx.Tr("auth.sign_up_successful"))
  942. handleSignInFull(ctx, u, false, true)
  943. }
  944. // Activate render activate user page
  945. func Activate(ctx *context.Context) {
  946. code := ctx.Query("code")
  947. if len(code) == 0 {
  948. ctx.Data["IsActivatePage"] = true
  949. if ctx.User.IsActive {
  950. ctx.Error(404)
  951. return
  952. }
  953. // Resend confirmation email.
  954. if setting.Service.RegisterEmailConfirm {
  955. if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
  956. ctx.Data["ResendLimited"] = true
  957. } else {
  958. ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  959. models.SendActivateAccountMail(ctx.Context, ctx.User)
  960. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  961. log.Error("Set cache(MailResendLimit) fail: %v", err)
  962. }
  963. }
  964. } else {
  965. ctx.Data["ServiceNotEnabled"] = true
  966. }
  967. ctx.HTML(200, TplActivate)
  968. return
  969. }
  970. // Verify code.
  971. if user := models.VerifyUserActiveCode(code); user != nil {
  972. user.IsActive = true
  973. var err error
  974. if user.Rands, err = models.GetUserSalt(); err != nil {
  975. ctx.ServerError("UpdateUser", err)
  976. return
  977. }
  978. if err := models.UpdateUserCols(user, "is_active", "rands"); err != nil {
  979. if models.IsErrUserNotExist(err) {
  980. ctx.Error(404)
  981. } else {
  982. ctx.ServerError("UpdateUser", err)
  983. }
  984. return
  985. }
  986. log.Trace("User activated: %s", user.Name)
  987. err = ctx.Session.Set("uid", user.ID)
  988. if err != nil {
  989. log.Error(fmt.Sprintf("Error setting session: %v", err))
  990. }
  991. err = ctx.Session.Set("uname", user.Name)
  992. if err != nil {
  993. log.Error(fmt.Sprintf("Error setting session: %v", err))
  994. }
  995. ctx.Flash.Success(ctx.Tr("auth.account_activated"))
  996. ctx.Redirect(setting.AppSubURL + "/")
  997. return
  998. }
  999. ctx.Data["IsActivateFailed"] = true
  1000. ctx.HTML(200, TplActivate)
  1001. }
  1002. // ActivateEmail render the activate email page
  1003. func ActivateEmail(ctx *context.Context) {
  1004. code := ctx.Query("code")
  1005. emailStr := ctx.Query("email")
  1006. // Verify code.
  1007. if email := models.VerifyActiveEmailCode(code, emailStr); email != nil {
  1008. if err := email.Activate(); err != nil {
  1009. ctx.ServerError("ActivateEmail", err)
  1010. }
  1011. log.Trace("Email activated: %s", email.Email)
  1012. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  1013. }
  1014. ctx.Redirect(setting.AppSubURL + "/user/settings/email")
  1015. }
  1016. // ForgotPasswd render the forget pasword page
  1017. func ForgotPasswd(ctx *context.Context) {
  1018. ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title")
  1019. if setting.MailService == nil {
  1020. ctx.Data["IsResetDisable"] = true
  1021. ctx.HTML(200, tplForgotPassword)
  1022. return
  1023. }
  1024. email := ctx.Query("email")
  1025. ctx.Data["Email"] = email
  1026. ctx.Data["IsResetRequest"] = true
  1027. ctx.HTML(200, tplForgotPassword)
  1028. }
  1029. // ForgotPasswdPost response for forget password request
  1030. func ForgotPasswdPost(ctx *context.Context) {
  1031. ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title")
  1032. if setting.MailService == nil {
  1033. ctx.NotFound("ForgotPasswdPost", nil)
  1034. return
  1035. }
  1036. ctx.Data["IsResetRequest"] = true
  1037. email := ctx.Query("email")
  1038. ctx.Data["Email"] = email
  1039. u, err := models.GetUserByEmail(email)
  1040. if err != nil {
  1041. if models.IsErrUserNotExist(err) {
  1042. ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
  1043. ctx.Data["IsResetSent"] = true
  1044. ctx.HTML(200, tplForgotPassword)
  1045. return
  1046. }
  1047. ctx.ServerError("user.ResetPasswd(check existence)", err)
  1048. return
  1049. }
  1050. if !u.IsLocal() && !u.IsOAuth2() {
  1051. ctx.Data["Err_Email"] = true
  1052. ctx.RenderWithErr(ctx.Tr("auth.non_local_account"), tplForgotPassword, nil)
  1053. return
  1054. }
  1055. if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) {
  1056. ctx.Data["ResendLimited"] = true
  1057. ctx.HTML(200, tplForgotPassword)
  1058. return
  1059. }
  1060. models.SendResetPasswordMail(ctx.Context, u)
  1061. if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  1062. log.Error("Set cache(MailResendLimit) fail: %v", err)
  1063. }
  1064. ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
  1065. ctx.Data["IsResetSent"] = true
  1066. ctx.HTML(200, tplForgotPassword)
  1067. }
  1068. func commonResetPassword(ctx *context.Context) *models.User {
  1069. code := ctx.Query("code")
  1070. ctx.Data["Title"] = ctx.Tr("auth.reset_password")
  1071. ctx.Data["Code"] = code
  1072. if nil != ctx.User {
  1073. ctx.Data["user_signed_in"] = true
  1074. }
  1075. if len(code) == 0 {
  1076. ctx.Flash.Error(ctx.Tr("auth.invalid_code"))
  1077. return nil
  1078. }
  1079. // Fail early, don't frustrate the user
  1080. u := models.VerifyUserActiveCode(code)
  1081. if u == nil {
  1082. ctx.Flash.Error(ctx.Tr("auth.invalid_code"))
  1083. return nil
  1084. }
  1085. // Show the user that they are affecting the account that they intended to
  1086. ctx.Data["user_email"] = u.Email
  1087. if nil != ctx.User && u.ID != ctx.User.ID {
  1088. ctx.Flash.Error(ctx.Tr("auth.reset_password_wrong_user", ctx.User.Email, u.Email))
  1089. return nil
  1090. }
  1091. return u
  1092. }
  1093. // ResetPasswd render the account recovery page
  1094. func ResetPasswd(ctx *context.Context) {
  1095. ctx.Data["IsResetForm"] = true
  1096. commonResetPassword(ctx)
  1097. ctx.HTML(200, tplResetPassword)
  1098. }
  1099. // ResetPasswdPost response from account recovery request
  1100. func ResetPasswdPost(ctx *context.Context) {
  1101. u := commonResetPassword(ctx)
  1102. if u == nil {
  1103. // Flash error has been set
  1104. ctx.HTML(200, tplResetPassword)
  1105. return
  1106. }
  1107. // Validate password length.
  1108. passwd := ctx.Query("password")
  1109. if len(passwd) < setting.MinPasswordLength {
  1110. ctx.Data["IsResetForm"] = true
  1111. ctx.Data["Err_Password"] = true
  1112. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplResetPassword, nil)
  1113. return
  1114. }
  1115. var err error
  1116. if u.Rands, err = models.GetUserSalt(); err != nil {
  1117. ctx.ServerError("UpdateUser", err)
  1118. return
  1119. }
  1120. if u.Salt, err = models.GetUserSalt(); err != nil {
  1121. ctx.ServerError("UpdateUser", err)
  1122. return
  1123. }
  1124. u.HashPassword(passwd)
  1125. u.MustChangePassword = false
  1126. if err := models.UpdateUserCols(u, "must_change_password", "passwd", "rands", "salt"); err != nil {
  1127. ctx.ServerError("UpdateUser", err)
  1128. return
  1129. }
  1130. log.Trace("User password reset: %s", u.Name)
  1131. ctx.Data["IsResetFailed"] = true
  1132. remember := len(ctx.Query("remember")) != 0
  1133. handleSignInFull(ctx, u, remember, true)
  1134. }
  1135. // MustChangePassword renders the page to change a user's password
  1136. func MustChangePassword(ctx *context.Context) {
  1137. ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
  1138. ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/settings/change_password"
  1139. ctx.HTML(200, tplMustChangePassword)
  1140. }
  1141. // MustChangePasswordPost response for updating a user's password after his/her
  1142. // account was created by an admin
  1143. func MustChangePasswordPost(ctx *context.Context, cpt *captcha.Captcha, form auth.MustChangePasswordForm) {
  1144. ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
  1145. ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/settings/change_password"
  1146. if ctx.HasError() {
  1147. ctx.HTML(200, tplMustChangePassword)
  1148. return
  1149. }
  1150. u := ctx.User
  1151. // Make sure only requests for users who are eligible to change their password via
  1152. // this method passes through
  1153. if !u.MustChangePassword {
  1154. ctx.ServerError("MustUpdatePassword", errors.New("cannot update password.. Please visit the settings page"))
  1155. return
  1156. }
  1157. if form.Password != form.Retype {
  1158. ctx.Data["Err_Password"] = true
  1159. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplMustChangePassword, &form)
  1160. return
  1161. }
  1162. if len(form.Password) < setting.MinPasswordLength {
  1163. ctx.Data["Err_Password"] = true
  1164. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplMustChangePassword, &form)
  1165. return
  1166. }
  1167. var err error
  1168. if u.Salt, err = models.GetUserSalt(); err != nil {
  1169. ctx.ServerError("UpdateUser", err)
  1170. return
  1171. }
  1172. u.HashPassword(form.Password)
  1173. u.MustChangePassword = false
  1174. if err := models.UpdateUserCols(u, "must_change_password", "passwd", "salt"); err != nil {
  1175. ctx.ServerError("UpdateUser", err)
  1176. return
  1177. }
  1178. ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
  1179. log.Trace("User updated password: %s", u.Name)
  1180. if redirectTo := ctx.GetCookie("redirect_to"); len(redirectTo) > 0 && !util.IsExternalURL(redirectTo) {
  1181. ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
  1182. ctx.RedirectToFirst(redirectTo)
  1183. return
  1184. }
  1185. ctx.Redirect(setting.AppSubURL + "/")
  1186. }