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

1291 lines
38 KiB

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