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 46 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
Add single sign-on support via SSPI on Windows (#8463) * Add single sign-on support via SSPI on Windows * Ensure plugins implement interface * Ensure plugins implement interface * Move functions used only by the SSPI auth method to sspi_windows.go * Field SSPISeparatorReplacement of AuthenticationForm should not be required via binding, as binding will insist the field is non-empty even if another login type is selected * Fix breaking of oauth authentication on download links. Do not create new session with SSPI authentication on download links. * Update documentation for the new 'SPNEGO with SSPI' login source * Mention in documentation that ROOT_URL should contain the FQDN of the server * Make sure that Contexter is not checking for active login sources when the ORM engine is not initialized (eg. when installing) * Always initialize and free SSO methods, even if they are not enabled, as a method can be activated while the app is running (from Authentication sources) * Add option in SSPIConfig for removing of domains from logon names * Update helper text for StripDomainNames option * Make sure handleSignIn() is called after a new user object is created by SSPI auth method * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Only make a query to the DB to check if SSPI is enabled on handlers that need that information for templates * Remove code duplication * Log errors in ActiveLoginSources Co-Authored-By: Lauris BH <lauris@nix.lv> * Revert suffix of randomly generated E-mails for Reverse proxy authentication Co-Authored-By: Lauris BH <lauris@nix.lv> * Revert unneeded white-space change in template Co-Authored-By: Lauris BH <lauris@nix.lv> * Add copyright comments at the top of new files * Use loopback name for randomly generated emails * Add locale tag for the SSPISeparatorReplacement field with proper casing * Revert casing of SSPISeparatorReplacement field in locale file, moving it up, next to other form fields * Update docs/content/doc/features/authentication.en-us.md Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Remove Priority() method and define the order in which SSO auth methods should be executed in one place * Log authenticated username only if it's not empty * Rephrase helper text for automatic creation of users * Return error if more than one active SSPI auth source is found * Change newUser() function to return error, letting caller log/handle the error * Move isPublicResource, isPublicPage and handleSignIn functions outside SSPI auth method to allow other SSO methods to reuse them if needed * Refactor initialization of the list containing SSO auth methods * Validate SSPI settings on POST * Change SSPI to only perform authentication on its own login page, API paths and download links. Leave Toggle middleware to redirect non authenticated users to login page * Make 'Default language' in SSPI config empty, unless changed by admin * Show error if admin tries to add a second authentication source of type SSPI * Simplify declaration of global variable * Rebuild gitgraph.js on Linux * Make sure config values containing only whitespace are not accepted
6 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
Add single sign-on support via SSPI on Windows (#8463) * Add single sign-on support via SSPI on Windows * Ensure plugins implement interface * Ensure plugins implement interface * Move functions used only by the SSPI auth method to sspi_windows.go * Field SSPISeparatorReplacement of AuthenticationForm should not be required via binding, as binding will insist the field is non-empty even if another login type is selected * Fix breaking of oauth authentication on download links. Do not create new session with SSPI authentication on download links. * Update documentation for the new 'SPNEGO with SSPI' login source * Mention in documentation that ROOT_URL should contain the FQDN of the server * Make sure that Contexter is not checking for active login sources when the ORM engine is not initialized (eg. when installing) * Always initialize and free SSO methods, even if they are not enabled, as a method can be activated while the app is running (from Authentication sources) * Add option in SSPIConfig for removing of domains from logon names * Update helper text for StripDomainNames option * Make sure handleSignIn() is called after a new user object is created by SSPI auth method * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Only make a query to the DB to check if SSPI is enabled on handlers that need that information for templates * Remove code duplication * Log errors in ActiveLoginSources Co-Authored-By: Lauris BH <lauris@nix.lv> * Revert suffix of randomly generated E-mails for Reverse proxy authentication Co-Authored-By: Lauris BH <lauris@nix.lv> * Revert unneeded white-space change in template Co-Authored-By: Lauris BH <lauris@nix.lv> * Add copyright comments at the top of new files * Use loopback name for randomly generated emails * Add locale tag for the SSPISeparatorReplacement field with proper casing * Revert casing of SSPISeparatorReplacement field in locale file, moving it up, next to other form fields * Update docs/content/doc/features/authentication.en-us.md Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Remove Priority() method and define the order in which SSO auth methods should be executed in one place * Log authenticated username only if it's not empty * Rephrase helper text for automatic creation of users * Return error if more than one active SSPI auth source is found * Change newUser() function to return error, letting caller log/handle the error * Move isPublicResource, isPublicPage and handleSignIn functions outside SSPI auth method to allow other SSO methods to reuse them if needed * Refactor initialization of the list containing SSO auth methods * Validate SSPI settings on POST * Change SSPI to only perform authentication on its own login page, API paths and download links. Leave Toggle middleware to redirect non authenticated users to login page * Make 'Default language' in SSPI config empty, unless changed by admin * Show error if admin tries to add a second authentication source of type SSPI * Simplify declaration of global variable * Rebuild gitgraph.js on Linux * Make sure config values containing only whitespace are not accepted
6 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
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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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/password"
  18. "code.gitea.io/gitea/modules/recaptcha"
  19. "code.gitea.io/gitea/modules/setting"
  20. "code.gitea.io/gitea/modules/timeutil"
  21. "code.gitea.io/gitea/modules/util"
  22. "code.gitea.io/gitea/services/externalaccount"
  23. "code.gitea.io/gitea/services/mailer"
  24. "gitea.com/macaron/captcha"
  25. "github.com/markbates/goth"
  26. "github.com/tstranex/u2f"
  27. )
  28. const (
  29. // tplMustChangePassword template for updating a user's password
  30. tplMustChangePassword = "user/auth/change_passwd"
  31. // tplSignIn template for sign in page
  32. tplSignIn base.TplName = "user/auth/signin"
  33. // tplSignUp template path for sign up page
  34. tplSignUp base.TplName = "user/auth/signup"
  35. // TplActivate template path for activate user
  36. TplActivate base.TplName = "user/auth/activate"
  37. tplForgotPassword base.TplName = "user/auth/forgot_passwd"
  38. tplResetPassword base.TplName = "user/auth/reset_passwd"
  39. tplTwofa base.TplName = "user/auth/twofa"
  40. tplTwofaScratch base.TplName = "user/auth/twofa_scratch"
  41. tplLinkAccount base.TplName = "user/auth/link_account"
  42. tplU2F base.TplName = "user/auth/u2f"
  43. )
  44. // AutoSignIn reads cookie and try to auto-login.
  45. func AutoSignIn(ctx *context.Context) (bool, error) {
  46. if !models.HasEngine {
  47. return false, nil
  48. }
  49. uname := ctx.GetCookie(setting.CookieUserName)
  50. if len(uname) == 0 {
  51. return false, nil
  52. }
  53. isSucceed := false
  54. defer func() {
  55. if !isSucceed {
  56. log.Trace("auto-login cookie cleared: %s", uname)
  57. ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
  58. ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
  59. }
  60. }()
  61. u, err := models.GetUserByName(uname)
  62. if err != nil {
  63. if !models.IsErrUserNotExist(err) {
  64. return false, fmt.Errorf("GetUserByName: %v", err)
  65. }
  66. return false, nil
  67. }
  68. if val, ok := ctx.GetSuperSecureCookie(
  69. base.EncodeMD5(u.Rands+u.Passwd), setting.CookieRememberName); !ok || val != u.Name {
  70. return false, nil
  71. }
  72. isSucceed = true
  73. err = ctx.Session.Set("uid", u.ID)
  74. if err != nil {
  75. return false, err
  76. }
  77. err = ctx.Session.Set("uname", u.Name)
  78. if err != nil {
  79. return false, err
  80. }
  81. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
  82. return true, nil
  83. }
  84. func checkAutoLogin(ctx *context.Context) bool {
  85. // Check auto-login.
  86. isSucceed, err := AutoSignIn(ctx)
  87. if err != nil {
  88. ctx.ServerError("AutoSignIn", err)
  89. return true
  90. }
  91. redirectTo := ctx.Query("redirect_to")
  92. if len(redirectTo) > 0 {
  93. ctx.SetCookie("redirect_to", redirectTo, 0, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  94. } else {
  95. redirectTo = ctx.GetCookie("redirect_to")
  96. }
  97. if isSucceed {
  98. ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  99. ctx.RedirectToFirst(redirectTo, setting.AppSubURL+string(setting.LandingPageURL))
  100. return true
  101. }
  102. return false
  103. }
  104. // SignIn render sign in page
  105. func SignIn(ctx *context.Context) {
  106. ctx.Data["Title"] = ctx.Tr("sign_in")
  107. // Check auto-login.
  108. if checkAutoLogin(ctx) {
  109. return
  110. }
  111. orderedOAuth2Names, oauth2Providers, err := models.GetActiveOAuth2Providers()
  112. if err != nil {
  113. ctx.ServerError("UserSignIn", err)
  114. return
  115. }
  116. ctx.Data["OrderedOAuth2Names"] = orderedOAuth2Names
  117. ctx.Data["OAuth2Providers"] = oauth2Providers
  118. ctx.Data["Title"] = ctx.Tr("sign_in")
  119. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login"
  120. ctx.Data["PageIsSignIn"] = true
  121. ctx.Data["PageIsLogin"] = true
  122. ctx.Data["EnableSSPI"] = models.IsSSPIEnabled()
  123. ctx.HTML(200, tplSignIn)
  124. }
  125. // SignInPost response for sign in request
  126. func SignInPost(ctx *context.Context, form auth.SignInForm) {
  127. ctx.Data["Title"] = ctx.Tr("sign_in")
  128. orderedOAuth2Names, oauth2Providers, err := models.GetActiveOAuth2Providers()
  129. if err != nil {
  130. ctx.ServerError("UserSignIn", err)
  131. return
  132. }
  133. ctx.Data["OrderedOAuth2Names"] = orderedOAuth2Names
  134. ctx.Data["OAuth2Providers"] = oauth2Providers
  135. ctx.Data["Title"] = ctx.Tr("sign_in")
  136. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login"
  137. ctx.Data["PageIsSignIn"] = true
  138. ctx.Data["PageIsLogin"] = true
  139. ctx.Data["EnableSSPI"] = models.IsSSPIEnabled()
  140. if ctx.HasError() {
  141. ctx.HTML(200, tplSignIn)
  142. return
  143. }
  144. u, err := models.UserSignIn(form.UserName, form.Password)
  145. if err != nil {
  146. if models.IsErrUserNotExist(err) {
  147. ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplSignIn, &form)
  148. log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr())
  149. } else if models.IsErrEmailAlreadyUsed(err) {
  150. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignIn, &form)
  151. log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr())
  152. } else if models.IsErrUserProhibitLogin(err) {
  153. log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr())
  154. ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
  155. ctx.HTML(200, "user/auth/prohibit_login")
  156. } else if models.IsErrUserInactive(err) {
  157. if setting.Service.RegisterEmailConfirm {
  158. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  159. ctx.HTML(200, TplActivate)
  160. } else {
  161. log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr())
  162. ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
  163. ctx.HTML(200, "user/auth/prohibit_login")
  164. }
  165. } else {
  166. ctx.ServerError("UserSignIn", err)
  167. }
  168. return
  169. }
  170. // If this user is enrolled in 2FA, we can't sign the user in just yet.
  171. // Instead, redirect them to the 2FA authentication page.
  172. _, err = models.GetTwoFactorByUID(u.ID)
  173. if err != nil {
  174. if models.IsErrTwoFactorNotEnrolled(err) {
  175. handleSignIn(ctx, u, form.Remember)
  176. } else {
  177. ctx.ServerError("UserSignIn", err)
  178. }
  179. return
  180. }
  181. // User needs to use 2FA, save data and redirect to 2FA page.
  182. err = ctx.Session.Set("twofaUid", u.ID)
  183. if err != nil {
  184. ctx.ServerError("UserSignIn", err)
  185. return
  186. }
  187. err = ctx.Session.Set("twofaRemember", form.Remember)
  188. if err != nil {
  189. ctx.ServerError("UserSignIn", err)
  190. return
  191. }
  192. regs, err := models.GetU2FRegistrationsByUID(u.ID)
  193. if err == nil && len(regs) > 0 {
  194. ctx.Redirect(setting.AppSubURL + "/user/u2f")
  195. return
  196. }
  197. ctx.Redirect(setting.AppSubURL + "/user/two_factor")
  198. }
  199. // TwoFactor shows the user a two-factor authentication page.
  200. func TwoFactor(ctx *context.Context) {
  201. ctx.Data["Title"] = ctx.Tr("twofa")
  202. // Check auto-login.
  203. if checkAutoLogin(ctx) {
  204. return
  205. }
  206. // Ensure user is in a 2FA session.
  207. if ctx.Session.Get("twofaUid") == nil {
  208. ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
  209. return
  210. }
  211. ctx.HTML(200, tplTwofa)
  212. }
  213. // TwoFactorPost validates a user's two-factor authentication token.
  214. func TwoFactorPost(ctx *context.Context, form auth.TwoFactorAuthForm) {
  215. ctx.Data["Title"] = ctx.Tr("twofa")
  216. // Ensure user is in a 2FA session.
  217. idSess := ctx.Session.Get("twofaUid")
  218. if idSess == nil {
  219. ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
  220. return
  221. }
  222. id := idSess.(int64)
  223. twofa, err := models.GetTwoFactorByUID(id)
  224. if err != nil {
  225. ctx.ServerError("UserSignIn", err)
  226. return
  227. }
  228. // Validate the passcode with the stored TOTP secret.
  229. ok, err := twofa.ValidateTOTP(form.Passcode)
  230. if err != nil {
  231. ctx.ServerError("UserSignIn", err)
  232. return
  233. }
  234. if ok && twofa.LastUsedPasscode != form.Passcode {
  235. remember := ctx.Session.Get("twofaRemember").(bool)
  236. u, err := models.GetUserByID(id)
  237. if err != nil {
  238. ctx.ServerError("UserSignIn", err)
  239. return
  240. }
  241. if ctx.Session.Get("linkAccount") != nil {
  242. gothUser := ctx.Session.Get("linkAccountGothUser")
  243. if gothUser == nil {
  244. ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
  245. return
  246. }
  247. err = externalaccount.LinkAccountToUser(u, gothUser.(goth.User))
  248. if err != nil {
  249. ctx.ServerError("UserSignIn", err)
  250. return
  251. }
  252. }
  253. twofa.LastUsedPasscode = form.Passcode
  254. if err = models.UpdateTwoFactor(twofa); err != nil {
  255. ctx.ServerError("UserSignIn", err)
  256. return
  257. }
  258. handleSignIn(ctx, u, remember)
  259. return
  260. }
  261. ctx.RenderWithErr(ctx.Tr("auth.twofa_passcode_incorrect"), tplTwofa, auth.TwoFactorAuthForm{})
  262. }
  263. // TwoFactorScratch shows the scratch code form for two-factor authentication.
  264. func TwoFactorScratch(ctx *context.Context) {
  265. ctx.Data["Title"] = ctx.Tr("twofa_scratch")
  266. // Check auto-login.
  267. if checkAutoLogin(ctx) {
  268. return
  269. }
  270. // Ensure user is in a 2FA session.
  271. if ctx.Session.Get("twofaUid") == nil {
  272. ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
  273. return
  274. }
  275. ctx.HTML(200, tplTwofaScratch)
  276. }
  277. // TwoFactorScratchPost validates and invalidates a user's two-factor scratch token.
  278. func TwoFactorScratchPost(ctx *context.Context, form auth.TwoFactorScratchAuthForm) {
  279. ctx.Data["Title"] = ctx.Tr("twofa_scratch")
  280. // Ensure user is in a 2FA session.
  281. idSess := ctx.Session.Get("twofaUid")
  282. if idSess == nil {
  283. ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
  284. return
  285. }
  286. id := idSess.(int64)
  287. twofa, err := models.GetTwoFactorByUID(id)
  288. if err != nil {
  289. ctx.ServerError("UserSignIn", err)
  290. return
  291. }
  292. // Validate the passcode with the stored TOTP secret.
  293. if twofa.VerifyScratchToken(form.Token) {
  294. // Invalidate the scratch token.
  295. _, err = twofa.GenerateScratchToken()
  296. if err != nil {
  297. ctx.ServerError("UserSignIn", err)
  298. return
  299. }
  300. if err = models.UpdateTwoFactor(twofa); err != nil {
  301. ctx.ServerError("UserSignIn", err)
  302. return
  303. }
  304. remember := ctx.Session.Get("twofaRemember").(bool)
  305. u, err := models.GetUserByID(id)
  306. if err != nil {
  307. ctx.ServerError("UserSignIn", err)
  308. return
  309. }
  310. handleSignInFull(ctx, u, remember, false)
  311. ctx.Flash.Info(ctx.Tr("auth.twofa_scratch_used"))
  312. ctx.Redirect(setting.AppSubURL + "/user/settings/security")
  313. return
  314. }
  315. ctx.RenderWithErr(ctx.Tr("auth.twofa_scratch_token_incorrect"), tplTwofaScratch, auth.TwoFactorScratchAuthForm{})
  316. }
  317. // U2F shows the U2F login page
  318. func U2F(ctx *context.Context) {
  319. ctx.Data["Title"] = ctx.Tr("twofa")
  320. ctx.Data["RequireU2F"] = true
  321. // Check auto-login.
  322. if checkAutoLogin(ctx) {
  323. return
  324. }
  325. // Ensure user is in a 2FA session.
  326. if ctx.Session.Get("twofaUid") == nil {
  327. ctx.ServerError("UserSignIn", errors.New("not in U2F session"))
  328. return
  329. }
  330. ctx.HTML(200, tplU2F)
  331. }
  332. // U2FChallenge submits a sign challenge to the browser
  333. func U2FChallenge(ctx *context.Context) {
  334. // Ensure user is in a U2F session.
  335. idSess := ctx.Session.Get("twofaUid")
  336. if idSess == nil {
  337. ctx.ServerError("UserSignIn", errors.New("not in U2F session"))
  338. return
  339. }
  340. id := idSess.(int64)
  341. regs, err := models.GetU2FRegistrationsByUID(id)
  342. if err != nil {
  343. ctx.ServerError("UserSignIn", err)
  344. return
  345. }
  346. if len(regs) == 0 {
  347. ctx.ServerError("UserSignIn", errors.New("no device registered"))
  348. return
  349. }
  350. challenge, err := u2f.NewChallenge(setting.U2F.AppID, setting.U2F.TrustedFacets)
  351. if err != nil {
  352. ctx.ServerError("u2f.NewChallenge", err)
  353. return
  354. }
  355. if err = ctx.Session.Set("u2fChallenge", challenge); err != nil {
  356. ctx.ServerError("UserSignIn", err)
  357. return
  358. }
  359. ctx.JSON(200, challenge.SignRequest(regs.ToRegistrations()))
  360. }
  361. // U2FSign authenticates the user by signResp
  362. func U2FSign(ctx *context.Context, signResp u2f.SignResponse) {
  363. challSess := ctx.Session.Get("u2fChallenge")
  364. idSess := ctx.Session.Get("twofaUid")
  365. if challSess == nil || idSess == nil {
  366. ctx.ServerError("UserSignIn", errors.New("not in U2F session"))
  367. return
  368. }
  369. challenge := challSess.(*u2f.Challenge)
  370. id := idSess.(int64)
  371. regs, err := models.GetU2FRegistrationsByUID(id)
  372. if err != nil {
  373. ctx.ServerError("UserSignIn", err)
  374. return
  375. }
  376. for _, reg := range regs {
  377. r, err := reg.Parse()
  378. if err != nil {
  379. log.Fatal("parsing u2f registration: %v", err)
  380. continue
  381. }
  382. newCounter, authErr := r.Authenticate(signResp, *challenge, reg.Counter)
  383. if authErr == nil {
  384. reg.Counter = newCounter
  385. user, err := models.GetUserByID(id)
  386. if err != nil {
  387. ctx.ServerError("UserSignIn", err)
  388. return
  389. }
  390. remember := ctx.Session.Get("twofaRemember").(bool)
  391. if err := reg.UpdateCounter(); err != nil {
  392. ctx.ServerError("UserSignIn", err)
  393. return
  394. }
  395. if ctx.Session.Get("linkAccount") != nil {
  396. gothUser := ctx.Session.Get("linkAccountGothUser")
  397. if gothUser == nil {
  398. ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
  399. return
  400. }
  401. err = externalaccount.LinkAccountToUser(user, gothUser.(goth.User))
  402. if err != nil {
  403. ctx.ServerError("UserSignIn", err)
  404. return
  405. }
  406. }
  407. redirect := handleSignInFull(ctx, user, remember, false)
  408. if redirect == "" {
  409. redirect = setting.AppSubURL + "/"
  410. }
  411. ctx.PlainText(200, []byte(redirect))
  412. return
  413. }
  414. }
  415. ctx.Error(401)
  416. }
  417. // This handles the final part of the sign-in process of the user.
  418. func handleSignIn(ctx *context.Context, u *models.User, remember bool) {
  419. handleSignInFull(ctx, u, remember, true)
  420. }
  421. func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyRedirect bool) string {
  422. if remember {
  423. days := 86400 * setting.LogInRememberDays
  424. ctx.SetCookie(setting.CookieUserName, u.Name, days, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
  425. ctx.SetSuperSecureCookie(base.EncodeMD5(u.Rands+u.Passwd),
  426. setting.CookieRememberName, u.Name, days, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
  427. }
  428. _ = ctx.Session.Delete("openid_verified_uri")
  429. _ = ctx.Session.Delete("openid_signin_remember")
  430. _ = ctx.Session.Delete("openid_determined_email")
  431. _ = ctx.Session.Delete("openid_determined_username")
  432. _ = ctx.Session.Delete("twofaUid")
  433. _ = ctx.Session.Delete("twofaRemember")
  434. _ = ctx.Session.Delete("u2fChallenge")
  435. _ = ctx.Session.Delete("linkAccount")
  436. err := ctx.Session.Set("uid", u.ID)
  437. if err != nil {
  438. log.Error(fmt.Sprintf("Error setting session: %v", err))
  439. }
  440. err = ctx.Session.Set("uname", u.Name)
  441. if err != nil {
  442. log.Error(fmt.Sprintf("Error setting session: %v", err))
  443. }
  444. // Language setting of the user overwrites the one previously set
  445. // If the user does not have a locale set, we save the current one.
  446. if len(u.Language) == 0 {
  447. u.Language = ctx.Locale.Language()
  448. if err := models.UpdateUserCols(u, "language"); err != nil {
  449. log.Error(fmt.Sprintf("Error updating user language [user: %d, locale: %s]", u.ID, u.Language))
  450. return setting.AppSubURL + "/"
  451. }
  452. }
  453. ctx.SetCookie("lang", u.Language, nil, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
  454. // Clear whatever CSRF has right now, force to generate a new one
  455. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
  456. // Register last login
  457. u.SetLastLogin()
  458. if err := models.UpdateUserCols(u, "last_login_unix"); err != nil {
  459. ctx.ServerError("UpdateUserCols", err)
  460. return setting.AppSubURL + "/"
  461. }
  462. if redirectTo := ctx.GetCookie("redirect_to"); len(redirectTo) > 0 && !util.IsExternalURL(redirectTo) {
  463. ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  464. if obeyRedirect {
  465. ctx.RedirectToFirst(redirectTo)
  466. }
  467. return redirectTo
  468. }
  469. if obeyRedirect {
  470. ctx.Redirect(setting.AppSubURL + "/")
  471. }
  472. return setting.AppSubURL + "/"
  473. }
  474. // SignInOAuth handles the OAuth2 login buttons
  475. func SignInOAuth(ctx *context.Context) {
  476. provider := ctx.Params(":provider")
  477. loginSource, err := models.GetActiveOAuth2LoginSourceByName(provider)
  478. if err != nil {
  479. ctx.ServerError("SignIn", err)
  480. return
  481. }
  482. // try to do a direct callback flow, so we don't authenticate the user again but use the valid accesstoken to get the user
  483. user, gothUser, err := oAuth2UserLoginCallback(loginSource, ctx.Req.Request, ctx.Resp)
  484. if err == nil && user != nil {
  485. // we got the user without going through the whole OAuth2 authentication flow again
  486. handleOAuth2SignIn(user, gothUser, ctx, err)
  487. return
  488. }
  489. err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp)
  490. if err != nil {
  491. ctx.ServerError("SignIn", err)
  492. }
  493. // redirect is done in oauth2.Auth
  494. }
  495. // SignInOAuthCallback handles the callback from the given provider
  496. func SignInOAuthCallback(ctx *context.Context) {
  497. provider := ctx.Params(":provider")
  498. // first look if the provider is still active
  499. loginSource, err := models.GetActiveOAuth2LoginSourceByName(provider)
  500. if err != nil {
  501. ctx.ServerError("SignIn", err)
  502. return
  503. }
  504. if loginSource == nil {
  505. ctx.ServerError("SignIn", errors.New("No valid provider found, check configured callback url in provider"))
  506. return
  507. }
  508. u, gothUser, err := oAuth2UserLoginCallback(loginSource, ctx.Req.Request, ctx.Resp)
  509. handleOAuth2SignIn(u, gothUser, ctx, err)
  510. }
  511. func handleOAuth2SignIn(u *models.User, gothUser goth.User, ctx *context.Context, err error) {
  512. if err != nil {
  513. ctx.ServerError("UserSignIn", err)
  514. return
  515. }
  516. if u == nil {
  517. // no existing user is found, request attach or new account
  518. err = ctx.Session.Set("linkAccountGothUser", gothUser)
  519. if err != nil {
  520. log.Error(fmt.Sprintf("Error setting session: %v", err))
  521. }
  522. ctx.Redirect(setting.AppSubURL + "/user/link_account")
  523. return
  524. }
  525. // If this user is enrolled in 2FA, we can't sign the user in just yet.
  526. // Instead, redirect them to the 2FA authentication page.
  527. _, err = models.GetTwoFactorByUID(u.ID)
  528. if err != nil {
  529. if !models.IsErrTwoFactorNotEnrolled(err) {
  530. ctx.ServerError("UserSignIn", err)
  531. return
  532. }
  533. err = ctx.Session.Set("uid", u.ID)
  534. if err != nil {
  535. log.Error(fmt.Sprintf("Error setting session: %v", err))
  536. }
  537. err = ctx.Session.Set("uname", u.Name)
  538. if err != nil {
  539. log.Error(fmt.Sprintf("Error setting session: %v", err))
  540. }
  541. // Clear whatever CSRF has right now, force to generate a new one
  542. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
  543. // Register last login
  544. u.SetLastLogin()
  545. if err := models.UpdateUserCols(u, "last_login_unix"); err != nil {
  546. ctx.ServerError("UpdateUserCols", err)
  547. return
  548. }
  549. // update external user information
  550. if err := models.UpdateExternalUser(u, gothUser); err != nil {
  551. log.Error("UpdateExternalUser failed: %v", err)
  552. }
  553. if redirectTo := ctx.GetCookie("redirect_to"); len(redirectTo) > 0 {
  554. ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  555. ctx.RedirectToFirst(redirectTo)
  556. return
  557. }
  558. ctx.Redirect(setting.AppSubURL + "/")
  559. return
  560. }
  561. // User needs to use 2FA, save data and redirect to 2FA page.
  562. err = ctx.Session.Set("twofaUid", u.ID)
  563. if err != nil {
  564. log.Error(fmt.Sprintf("Error setting session: %v", err))
  565. }
  566. err = ctx.Session.Set("twofaRemember", false)
  567. if err != nil {
  568. log.Error(fmt.Sprintf("Error setting session: %v", err))
  569. }
  570. // If U2F is enrolled -> Redirect to U2F instead
  571. regs, err := models.GetU2FRegistrationsByUID(u.ID)
  572. if err == nil && len(regs) > 0 {
  573. ctx.Redirect(setting.AppSubURL + "/user/u2f")
  574. return
  575. }
  576. ctx.Redirect(setting.AppSubURL + "/user/two_factor")
  577. }
  578. // OAuth2UserLoginCallback attempts to handle the callback from the OAuth2 provider and if successful
  579. // login the user
  580. func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Request, response http.ResponseWriter) (*models.User, goth.User, error) {
  581. gothUser, err := oauth2.ProviderCallback(loginSource.Name, request, response)
  582. if err != nil {
  583. return nil, goth.User{}, err
  584. }
  585. user := &models.User{
  586. LoginName: gothUser.UserID,
  587. LoginType: models.LoginOAuth2,
  588. LoginSource: loginSource.ID,
  589. }
  590. hasUser, err := models.GetUser(user)
  591. if err != nil {
  592. return nil, goth.User{}, err
  593. }
  594. if hasUser {
  595. return user, gothUser, nil
  596. }
  597. // search in external linked users
  598. externalLoginUser := &models.ExternalLoginUser{
  599. ExternalID: gothUser.UserID,
  600. LoginSourceID: loginSource.ID,
  601. }
  602. hasUser, err = models.GetExternalLogin(externalLoginUser)
  603. if err != nil {
  604. return nil, goth.User{}, err
  605. }
  606. if hasUser {
  607. user, err = models.GetUserByID(externalLoginUser.UserID)
  608. return user, gothUser, err
  609. }
  610. // no user found to login
  611. return nil, gothUser, nil
  612. }
  613. // LinkAccount shows the page where the user can decide to login or create a new account
  614. func LinkAccount(ctx *context.Context) {
  615. ctx.Data["DisablePassword"] = !setting.Service.RequireExternalRegistrationPassword || setting.Service.AllowOnlyExternalRegistration
  616. ctx.Data["Title"] = ctx.Tr("link_account")
  617. ctx.Data["LinkAccountMode"] = true
  618. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha && setting.Service.RequireExternalRegistrationCaptcha
  619. ctx.Data["CaptchaType"] = setting.Service.CaptchaType
  620. ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
  621. ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
  622. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  623. ctx.Data["ShowRegistrationButton"] = false
  624. // use this to set the right link into the signIn and signUp templates in the link_account template
  625. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
  626. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
  627. gothUser := ctx.Session.Get("linkAccountGothUser")
  628. if gothUser == nil {
  629. ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
  630. return
  631. }
  632. uname := gothUser.(goth.User).NickName
  633. email := gothUser.(goth.User).Email
  634. ctx.Data["user_name"] = uname
  635. ctx.Data["email"] = email
  636. if len(email) != 0 {
  637. u, err := models.GetUserByEmail(email)
  638. if err != nil && !models.IsErrUserNotExist(err) {
  639. ctx.ServerError("UserSignIn", err)
  640. return
  641. }
  642. if u != nil {
  643. ctx.Data["user_exists"] = true
  644. }
  645. } else if len(uname) != 0 {
  646. u, err := models.GetUserByName(uname)
  647. if err != nil && !models.IsErrUserNotExist(err) {
  648. ctx.ServerError("UserSignIn", err)
  649. return
  650. }
  651. if u != nil {
  652. ctx.Data["user_exists"] = true
  653. }
  654. }
  655. ctx.HTML(200, tplLinkAccount)
  656. }
  657. // LinkAccountPostSignIn handle the coupling of external account with another account using signIn
  658. func LinkAccountPostSignIn(ctx *context.Context, signInForm auth.SignInForm) {
  659. ctx.Data["DisablePassword"] = !setting.Service.RequireExternalRegistrationPassword || setting.Service.AllowOnlyExternalRegistration
  660. ctx.Data["Title"] = ctx.Tr("link_account")
  661. ctx.Data["LinkAccountMode"] = true
  662. ctx.Data["LinkAccountModeSignIn"] = true
  663. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha && setting.Service.RequireExternalRegistrationCaptcha
  664. ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
  665. ctx.Data["CaptchaType"] = setting.Service.CaptchaType
  666. ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
  667. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  668. ctx.Data["ShowRegistrationButton"] = false
  669. // use this to set the right link into the signIn and signUp templates in the link_account template
  670. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
  671. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
  672. gothUser := ctx.Session.Get("linkAccountGothUser")
  673. if gothUser == nil {
  674. ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
  675. return
  676. }
  677. if ctx.HasError() {
  678. ctx.HTML(200, tplLinkAccount)
  679. return
  680. }
  681. u, err := models.UserSignIn(signInForm.UserName, signInForm.Password)
  682. if err != nil {
  683. if models.IsErrUserNotExist(err) {
  684. ctx.Data["user_exists"] = true
  685. ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplLinkAccount, &signInForm)
  686. } else {
  687. ctx.ServerError("UserLinkAccount", err)
  688. }
  689. return
  690. }
  691. // If this user is enrolled in 2FA, we can't sign the user in just yet.
  692. // Instead, redirect them to the 2FA authentication page.
  693. _, err = models.GetTwoFactorByUID(u.ID)
  694. if err != nil {
  695. if !models.IsErrTwoFactorNotEnrolled(err) {
  696. ctx.ServerError("UserLinkAccount", err)
  697. return
  698. }
  699. err = externalaccount.LinkAccountToUser(u, gothUser.(goth.User))
  700. if err != nil {
  701. ctx.ServerError("UserLinkAccount", err)
  702. return
  703. }
  704. handleSignIn(ctx, u, signInForm.Remember)
  705. return
  706. }
  707. // User needs to use 2FA, save data and redirect to 2FA page.
  708. err = ctx.Session.Set("twofaUid", u.ID)
  709. if err != nil {
  710. log.Error(fmt.Sprintf("Error setting session: %v", err))
  711. }
  712. err = ctx.Session.Set("twofaRemember", signInForm.Remember)
  713. if err != nil {
  714. log.Error(fmt.Sprintf("Error setting session: %v", err))
  715. }
  716. err = ctx.Session.Set("linkAccount", true)
  717. if err != nil {
  718. log.Error(fmt.Sprintf("Error setting session: %v", err))
  719. }
  720. // If U2F is enrolled -> Redirect to U2F instead
  721. regs, err := models.GetU2FRegistrationsByUID(u.ID)
  722. if err == nil && len(regs) > 0 {
  723. ctx.Redirect(setting.AppSubURL + "/user/u2f")
  724. return
  725. }
  726. ctx.Redirect(setting.AppSubURL + "/user/two_factor")
  727. }
  728. // LinkAccountPostRegister handle the creation of a new account for an external account using signUp
  729. func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
  730. // TODO Make insecure passwords optional for local accounts also,
  731. // once email-based Second-Factor Auth is available
  732. ctx.Data["DisablePassword"] = !setting.Service.RequireExternalRegistrationPassword || setting.Service.AllowOnlyExternalRegistration
  733. ctx.Data["Title"] = ctx.Tr("link_account")
  734. ctx.Data["LinkAccountMode"] = true
  735. ctx.Data["LinkAccountModeRegister"] = true
  736. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha && setting.Service.RequireExternalRegistrationCaptcha
  737. ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
  738. ctx.Data["CaptchaType"] = setting.Service.CaptchaType
  739. ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
  740. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  741. ctx.Data["ShowRegistrationButton"] = false
  742. // use this to set the right link into the signIn and signUp templates in the link_account template
  743. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
  744. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
  745. gothUser := ctx.Session.Get("linkAccountGothUser")
  746. if gothUser == nil {
  747. ctx.ServerError("UserSignUp", errors.New("not in LinkAccount session"))
  748. return
  749. }
  750. if ctx.HasError() {
  751. ctx.HTML(200, tplLinkAccount)
  752. return
  753. }
  754. if setting.Service.DisableRegistration {
  755. ctx.Error(403)
  756. return
  757. }
  758. if setting.Service.EnableCaptcha && setting.Service.RequireExternalRegistrationCaptcha {
  759. var valid bool
  760. switch setting.Service.CaptchaType {
  761. case setting.ImageCaptcha:
  762. valid = cpt.VerifyReq(ctx.Req)
  763. case setting.ReCaptcha:
  764. valid, _ = recaptcha.Verify(form.GRecaptchaResponse)
  765. default:
  766. ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
  767. return
  768. }
  769. if !valid {
  770. ctx.Data["Err_Captcha"] = true
  771. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplLinkAccount, &form)
  772. return
  773. }
  774. }
  775. if setting.Service.AllowOnlyExternalRegistration || !setting.Service.RequireExternalRegistrationPassword {
  776. // In models.User an empty password is classed as not set, so we set form.Password to empty.
  777. // Eventually the database should be changed to indicate "Second Factor"-enabled accounts
  778. // (accounts that do not introduce the security vulnerabilities of a password).
  779. // If a user decides to circumvent second-factor security, and purposefully create a password,
  780. // they can still do so using the "Recover Account" option.
  781. form.Password = ""
  782. } else {
  783. if (len(strings.TrimSpace(form.Password)) > 0 || len(strings.TrimSpace(form.Retype)) > 0) && form.Password != form.Retype {
  784. ctx.Data["Err_Password"] = true
  785. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplLinkAccount, &form)
  786. return
  787. }
  788. if len(strings.TrimSpace(form.Password)) > 0 && len(form.Password) < setting.MinPasswordLength {
  789. ctx.Data["Err_Password"] = true
  790. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplLinkAccount, &form)
  791. return
  792. }
  793. }
  794. loginSource, err := models.GetActiveOAuth2LoginSourceByName(gothUser.(goth.User).Provider)
  795. if err != nil {
  796. ctx.ServerError("CreateUser", err)
  797. }
  798. u := &models.User{
  799. Name: form.UserName,
  800. Email: form.Email,
  801. Passwd: form.Password,
  802. IsActive: !setting.Service.RegisterEmailConfirm,
  803. LoginType: models.LoginOAuth2,
  804. LoginSource: loginSource.ID,
  805. LoginName: gothUser.(goth.User).UserID,
  806. }
  807. if err := models.CreateUser(u); err != nil {
  808. switch {
  809. case models.IsErrUserAlreadyExist(err):
  810. ctx.Data["Err_UserName"] = true
  811. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplLinkAccount, &form)
  812. case models.IsErrEmailAlreadyUsed(err):
  813. ctx.Data["Err_Email"] = true
  814. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplLinkAccount, &form)
  815. case models.IsErrNameReserved(err):
  816. ctx.Data["Err_UserName"] = true
  817. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplLinkAccount, &form)
  818. case models.IsErrNamePatternNotAllowed(err):
  819. ctx.Data["Err_UserName"] = true
  820. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplLinkAccount, &form)
  821. default:
  822. ctx.ServerError("CreateUser", err)
  823. }
  824. return
  825. }
  826. log.Trace("Account created: %s", u.Name)
  827. // Auto-set admin for the only user.
  828. if models.CountUsers() == 1 {
  829. u.IsAdmin = true
  830. u.IsActive = true
  831. u.SetLastLogin()
  832. if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
  833. ctx.ServerError("UpdateUser", err)
  834. return
  835. }
  836. }
  837. // update external user information
  838. if err := models.UpdateExternalUser(u, gothUser.(goth.User)); err != nil {
  839. log.Error("UpdateExternalUser failed: %v", err)
  840. }
  841. // Send confirmation email
  842. if setting.Service.RegisterEmailConfirm && u.ID > 1 {
  843. mailer.SendActivateAccountMail(ctx.Locale, u)
  844. ctx.Data["IsSendRegisterMail"] = true
  845. ctx.Data["Email"] = u.Email
  846. ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  847. ctx.HTML(200, TplActivate)
  848. if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  849. log.Error("Set cache(MailResendLimit) fail: %v", err)
  850. }
  851. return
  852. }
  853. ctx.Redirect(setting.AppSubURL + "/user/login")
  854. }
  855. func handleSignOut(ctx *context.Context) {
  856. _ = ctx.Session.Delete("uid")
  857. _ = ctx.Session.Delete("uname")
  858. _ = ctx.Session.Delete("socialId")
  859. _ = ctx.Session.Delete("socialName")
  860. _ = ctx.Session.Delete("socialEmail")
  861. ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
  862. ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
  863. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
  864. ctx.SetCookie("lang", "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true) // Setting the lang cookie will trigger the middleware to reset the language ot previous state.
  865. }
  866. // SignOut sign out from login status
  867. func SignOut(ctx *context.Context) {
  868. handleSignOut(ctx)
  869. ctx.Redirect(setting.AppSubURL + "/")
  870. }
  871. // SignUp render the register page
  872. func SignUp(ctx *context.Context) {
  873. ctx.Data["Title"] = ctx.Tr("sign_up")
  874. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
  875. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  876. ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
  877. ctx.Data["CaptchaType"] = setting.Service.CaptchaType
  878. ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
  879. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  880. ctx.HTML(200, tplSignUp)
  881. }
  882. // SignUpPost response for sign up information submission
  883. func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
  884. ctx.Data["Title"] = ctx.Tr("sign_up")
  885. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
  886. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  887. ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
  888. ctx.Data["CaptchaType"] = setting.Service.CaptchaType
  889. ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
  890. //Permission denied if DisableRegistration or AllowOnlyExternalRegistration options are true
  891. if setting.Service.DisableRegistration {
  892. ctx.Error(403)
  893. return
  894. }
  895. if ctx.HasError() {
  896. ctx.HTML(200, tplSignUp)
  897. return
  898. }
  899. if setting.Service.EnableCaptcha {
  900. var valid bool
  901. switch setting.Service.CaptchaType {
  902. case setting.ImageCaptcha:
  903. valid = cpt.VerifyReq(ctx.Req)
  904. case setting.ReCaptcha:
  905. valid, _ = recaptcha.Verify(form.GRecaptchaResponse)
  906. default:
  907. ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
  908. return
  909. }
  910. if !valid {
  911. ctx.Data["Err_Captcha"] = true
  912. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplSignUp, &form)
  913. return
  914. }
  915. }
  916. if !form.IsEmailDomainWhitelisted() {
  917. ctx.RenderWithErr(ctx.Tr("auth.email_domain_blacklisted"), tplSignUp, &form)
  918. return
  919. }
  920. if form.Password != form.Retype {
  921. ctx.Data["Err_Password"] = true
  922. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplSignUp, &form)
  923. return
  924. }
  925. if len(form.Password) < setting.MinPasswordLength {
  926. ctx.Data["Err_Password"] = true
  927. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplSignUp, &form)
  928. return
  929. }
  930. if !password.IsComplexEnough(form.Password) {
  931. ctx.Data["Err_Password"] = true
  932. ctx.RenderWithErr(password.BuildComplexityError(ctx), tplSignUp, &form)
  933. return
  934. }
  935. u := &models.User{
  936. Name: form.UserName,
  937. Email: form.Email,
  938. Passwd: form.Password,
  939. IsActive: !setting.Service.RegisterEmailConfirm,
  940. }
  941. if err := models.CreateUser(u); err != nil {
  942. switch {
  943. case models.IsErrUserAlreadyExist(err):
  944. ctx.Data["Err_UserName"] = true
  945. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplSignUp, &form)
  946. case models.IsErrEmailAlreadyUsed(err):
  947. ctx.Data["Err_Email"] = true
  948. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignUp, &form)
  949. case models.IsErrNameReserved(err):
  950. ctx.Data["Err_UserName"] = true
  951. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplSignUp, &form)
  952. case models.IsErrNamePatternNotAllowed(err):
  953. ctx.Data["Err_UserName"] = true
  954. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplSignUp, &form)
  955. default:
  956. ctx.ServerError("CreateUser", err)
  957. }
  958. return
  959. }
  960. log.Trace("Account created: %s", u.Name)
  961. // Auto-set admin for the only user.
  962. if models.CountUsers() == 1 {
  963. u.IsAdmin = true
  964. u.IsActive = true
  965. u.SetLastLogin()
  966. if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
  967. ctx.ServerError("UpdateUser", err)
  968. return
  969. }
  970. }
  971. // Send confirmation email, no need for social account.
  972. if setting.Service.RegisterEmailConfirm && u.ID > 1 {
  973. mailer.SendActivateAccountMail(ctx.Locale, u)
  974. ctx.Data["IsSendRegisterMail"] = true
  975. ctx.Data["Email"] = u.Email
  976. ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  977. ctx.HTML(200, TplActivate)
  978. if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  979. log.Error("Set cache(MailResendLimit) fail: %v", err)
  980. }
  981. return
  982. }
  983. ctx.Flash.Success(ctx.Tr("auth.sign_up_successful"))
  984. handleSignInFull(ctx, u, false, true)
  985. }
  986. // Activate render activate user page
  987. func Activate(ctx *context.Context) {
  988. code := ctx.Query("code")
  989. if len(code) == 0 {
  990. ctx.Data["IsActivatePage"] = true
  991. if ctx.User.IsActive {
  992. ctx.Error(404)
  993. return
  994. }
  995. // Resend confirmation email.
  996. if setting.Service.RegisterEmailConfirm {
  997. if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
  998. ctx.Data["ResendLimited"] = true
  999. } else {
  1000. ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  1001. mailer.SendActivateAccountMail(ctx.Locale, ctx.User)
  1002. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  1003. log.Error("Set cache(MailResendLimit) fail: %v", err)
  1004. }
  1005. }
  1006. } else {
  1007. ctx.Data["ServiceNotEnabled"] = true
  1008. }
  1009. ctx.HTML(200, TplActivate)
  1010. return
  1011. }
  1012. // Verify code.
  1013. if user := models.VerifyUserActiveCode(code); user != nil {
  1014. user.IsActive = true
  1015. var err error
  1016. if user.Rands, err = models.GetUserSalt(); err != nil {
  1017. ctx.ServerError("UpdateUser", err)
  1018. return
  1019. }
  1020. if err := models.UpdateUserCols(user, "is_active", "rands"); err != nil {
  1021. if models.IsErrUserNotExist(err) {
  1022. ctx.Error(404)
  1023. } else {
  1024. ctx.ServerError("UpdateUser", err)
  1025. }
  1026. return
  1027. }
  1028. log.Trace("User activated: %s", user.Name)
  1029. err = ctx.Session.Set("uid", user.ID)
  1030. if err != nil {
  1031. log.Error(fmt.Sprintf("Error setting session: %v", err))
  1032. }
  1033. err = ctx.Session.Set("uname", user.Name)
  1034. if err != nil {
  1035. log.Error(fmt.Sprintf("Error setting session: %v", err))
  1036. }
  1037. ctx.Flash.Success(ctx.Tr("auth.account_activated"))
  1038. ctx.Redirect(setting.AppSubURL + "/")
  1039. return
  1040. }
  1041. ctx.Data["IsActivateFailed"] = true
  1042. ctx.HTML(200, TplActivate)
  1043. }
  1044. // ActivateEmail render the activate email page
  1045. func ActivateEmail(ctx *context.Context) {
  1046. code := ctx.Query("code")
  1047. emailStr := ctx.Query("email")
  1048. // Verify code.
  1049. if email := models.VerifyActiveEmailCode(code, emailStr); email != nil {
  1050. if err := email.Activate(); err != nil {
  1051. ctx.ServerError("ActivateEmail", err)
  1052. }
  1053. log.Trace("Email activated: %s", email.Email)
  1054. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  1055. }
  1056. ctx.Redirect(setting.AppSubURL + "/user/settings/email")
  1057. }
  1058. // ForgotPasswd render the forget pasword page
  1059. func ForgotPasswd(ctx *context.Context) {
  1060. ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title")
  1061. if setting.MailService == nil {
  1062. ctx.Data["IsResetDisable"] = true
  1063. ctx.HTML(200, tplForgotPassword)
  1064. return
  1065. }
  1066. email := ctx.Query("email")
  1067. ctx.Data["Email"] = email
  1068. ctx.Data["IsResetRequest"] = true
  1069. ctx.HTML(200, tplForgotPassword)
  1070. }
  1071. // ForgotPasswdPost response for forget password request
  1072. func ForgotPasswdPost(ctx *context.Context) {
  1073. ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title")
  1074. if setting.MailService == nil {
  1075. ctx.NotFound("ForgotPasswdPost", nil)
  1076. return
  1077. }
  1078. ctx.Data["IsResetRequest"] = true
  1079. email := ctx.Query("email")
  1080. ctx.Data["Email"] = email
  1081. u, err := models.GetUserByEmail(email)
  1082. if err != nil {
  1083. if models.IsErrUserNotExist(err) {
  1084. ctx.Data["ResetPwdCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
  1085. ctx.Data["IsResetSent"] = true
  1086. ctx.HTML(200, tplForgotPassword)
  1087. return
  1088. }
  1089. ctx.ServerError("user.ResetPasswd(check existence)", err)
  1090. return
  1091. }
  1092. if !u.IsLocal() && !u.IsOAuth2() {
  1093. ctx.Data["Err_Email"] = true
  1094. ctx.RenderWithErr(ctx.Tr("auth.non_local_account"), tplForgotPassword, nil)
  1095. return
  1096. }
  1097. if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) {
  1098. ctx.Data["ResendLimited"] = true
  1099. ctx.HTML(200, tplForgotPassword)
  1100. return
  1101. }
  1102. mailer.SendResetPasswordMail(ctx.Locale, u)
  1103. if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  1104. log.Error("Set cache(MailResendLimit) fail: %v", err)
  1105. }
  1106. ctx.Data["ResetPwdCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
  1107. ctx.Data["IsResetSent"] = true
  1108. ctx.HTML(200, tplForgotPassword)
  1109. }
  1110. func commonResetPassword(ctx *context.Context) (*models.User, *models.TwoFactor) {
  1111. code := ctx.Query("code")
  1112. ctx.Data["Title"] = ctx.Tr("auth.reset_password")
  1113. ctx.Data["Code"] = code
  1114. if nil != ctx.User {
  1115. ctx.Data["user_signed_in"] = true
  1116. }
  1117. if len(code) == 0 {
  1118. ctx.Flash.Error(ctx.Tr("auth.invalid_code"))
  1119. return nil, nil
  1120. }
  1121. // Fail early, don't frustrate the user
  1122. u := models.VerifyUserActiveCode(code)
  1123. if u == nil {
  1124. ctx.Flash.Error(ctx.Tr("auth.invalid_code"))
  1125. return nil, nil
  1126. }
  1127. twofa, err := models.GetTwoFactorByUID(u.ID)
  1128. if err != nil {
  1129. if !models.IsErrTwoFactorNotEnrolled(err) {
  1130. ctx.Error(http.StatusInternalServerError, "CommonResetPassword", err.Error())
  1131. return nil, nil
  1132. }
  1133. } else {
  1134. ctx.Data["has_two_factor"] = true
  1135. ctx.Data["scratch_code"] = ctx.QueryBool("scratch_code")
  1136. }
  1137. // Show the user that they are affecting the account that they intended to
  1138. ctx.Data["user_email"] = u.Email
  1139. if nil != ctx.User && u.ID != ctx.User.ID {
  1140. ctx.Flash.Error(ctx.Tr("auth.reset_password_wrong_user", ctx.User.Email, u.Email))
  1141. return nil, nil
  1142. }
  1143. return u, twofa
  1144. }
  1145. // ResetPasswd render the account recovery page
  1146. func ResetPasswd(ctx *context.Context) {
  1147. ctx.Data["IsResetForm"] = true
  1148. commonResetPassword(ctx)
  1149. if ctx.Written() {
  1150. return
  1151. }
  1152. ctx.HTML(200, tplResetPassword)
  1153. }
  1154. // ResetPasswdPost response from account recovery request
  1155. func ResetPasswdPost(ctx *context.Context) {
  1156. u, twofa := commonResetPassword(ctx)
  1157. if ctx.Written() {
  1158. return
  1159. }
  1160. if u == nil {
  1161. // Flash error has been set
  1162. ctx.HTML(200, tplResetPassword)
  1163. return
  1164. }
  1165. // Validate password length.
  1166. passwd := ctx.Query("password")
  1167. if len(passwd) < setting.MinPasswordLength {
  1168. ctx.Data["IsResetForm"] = true
  1169. ctx.Data["Err_Password"] = true
  1170. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplResetPassword, nil)
  1171. return
  1172. } else if !password.IsComplexEnough(passwd) {
  1173. ctx.Data["IsResetForm"] = true
  1174. ctx.Data["Err_Password"] = true
  1175. ctx.RenderWithErr(password.BuildComplexityError(ctx), tplResetPassword, nil)
  1176. return
  1177. }
  1178. // Handle two-factor
  1179. regenerateScratchToken := false
  1180. if twofa != nil {
  1181. if ctx.QueryBool("scratch_code") {
  1182. if !twofa.VerifyScratchToken(ctx.Query("token")) {
  1183. ctx.Data["IsResetForm"] = true
  1184. ctx.Data["Err_Token"] = true
  1185. ctx.RenderWithErr(ctx.Tr("auth.twofa_scratch_token_incorrect"), tplResetPassword, nil)
  1186. return
  1187. }
  1188. regenerateScratchToken = true
  1189. } else {
  1190. passcode := ctx.Query("passcode")
  1191. ok, err := twofa.ValidateTOTP(passcode)
  1192. if err != nil {
  1193. ctx.Error(http.StatusInternalServerError, "ValidateTOTP", err.Error())
  1194. return
  1195. }
  1196. if !ok || twofa.LastUsedPasscode == passcode {
  1197. ctx.Data["IsResetForm"] = true
  1198. ctx.Data["Err_Passcode"] = true
  1199. ctx.RenderWithErr(ctx.Tr("auth.twofa_passcode_incorrect"), tplResetPassword, nil)
  1200. return
  1201. }
  1202. twofa.LastUsedPasscode = passcode
  1203. if err = models.UpdateTwoFactor(twofa); err != nil {
  1204. ctx.ServerError("ResetPasswdPost: UpdateTwoFactor", err)
  1205. return
  1206. }
  1207. }
  1208. }
  1209. var err error
  1210. if u.Rands, err = models.GetUserSalt(); err != nil {
  1211. ctx.ServerError("UpdateUser", err)
  1212. return
  1213. }
  1214. if u.Salt, err = models.GetUserSalt(); err != nil {
  1215. ctx.ServerError("UpdateUser", err)
  1216. return
  1217. }
  1218. u.HashPassword(passwd)
  1219. u.MustChangePassword = false
  1220. if err := models.UpdateUserCols(u, "must_change_password", "passwd", "rands", "salt"); err != nil {
  1221. ctx.ServerError("UpdateUser", err)
  1222. return
  1223. }
  1224. log.Trace("User password reset: %s", u.Name)
  1225. ctx.Data["IsResetFailed"] = true
  1226. remember := len(ctx.Query("remember")) != 0
  1227. if regenerateScratchToken {
  1228. // Invalidate the scratch token.
  1229. _, err = twofa.GenerateScratchToken()
  1230. if err != nil {
  1231. ctx.ServerError("UserSignIn", err)
  1232. return
  1233. }
  1234. if err = models.UpdateTwoFactor(twofa); err != nil {
  1235. ctx.ServerError("UserSignIn", err)
  1236. return
  1237. }
  1238. handleSignInFull(ctx, u, remember, false)
  1239. ctx.Flash.Info(ctx.Tr("auth.twofa_scratch_used"))
  1240. ctx.Redirect(setting.AppSubURL + "/user/settings/security")
  1241. return
  1242. }
  1243. handleSignInFull(ctx, u, remember, true)
  1244. }
  1245. // MustChangePassword renders the page to change a user's password
  1246. func MustChangePassword(ctx *context.Context) {
  1247. ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
  1248. ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/settings/change_password"
  1249. ctx.HTML(200, tplMustChangePassword)
  1250. }
  1251. // MustChangePasswordPost response for updating a user's password after his/her
  1252. // account was created by an admin
  1253. func MustChangePasswordPost(ctx *context.Context, cpt *captcha.Captcha, form auth.MustChangePasswordForm) {
  1254. ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
  1255. ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/settings/change_password"
  1256. if ctx.HasError() {
  1257. ctx.HTML(200, tplMustChangePassword)
  1258. return
  1259. }
  1260. u := ctx.User
  1261. // Make sure only requests for users who are eligible to change their password via
  1262. // this method passes through
  1263. if !u.MustChangePassword {
  1264. ctx.ServerError("MustUpdatePassword", errors.New("cannot update password.. Please visit the settings page"))
  1265. return
  1266. }
  1267. if form.Password != form.Retype {
  1268. ctx.Data["Err_Password"] = true
  1269. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplMustChangePassword, &form)
  1270. return
  1271. }
  1272. if len(form.Password) < setting.MinPasswordLength {
  1273. ctx.Data["Err_Password"] = true
  1274. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplMustChangePassword, &form)
  1275. return
  1276. }
  1277. var err error
  1278. if u.Salt, err = models.GetUserSalt(); err != nil {
  1279. ctx.ServerError("UpdateUser", err)
  1280. return
  1281. }
  1282. u.HashPassword(form.Password)
  1283. u.MustChangePassword = false
  1284. if err := models.UpdateUserCols(u, "must_change_password", "passwd", "salt"); err != nil {
  1285. ctx.ServerError("UpdateUser", err)
  1286. return
  1287. }
  1288. ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
  1289. log.Trace("User updated password: %s", u.Name)
  1290. if redirectTo := ctx.GetCookie("redirect_to"); len(redirectTo) > 0 && !util.IsExternalURL(redirectTo) {
  1291. ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
  1292. ctx.RedirectToFirst(redirectTo)
  1293. return
  1294. }
  1295. ctx.Redirect(setting.AppSubURL + "/")
  1296. }