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

auth.go 30 kB

Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 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)
8 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. "errors"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/auth"
  13. "code.gitea.io/gitea/modules/auth/oauth2"
  14. "code.gitea.io/gitea/modules/base"
  15. "code.gitea.io/gitea/modules/context"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/setting"
  18. "github.com/go-macaron/captcha"
  19. "github.com/markbates/goth"
  20. )
  21. const (
  22. // tplSignIn template for sign in page
  23. tplSignIn base.TplName = "user/auth/signin"
  24. // tplSignUp template path for sign up page
  25. tplSignUp base.TplName = "user/auth/signup"
  26. // TplActivate template path for activate user
  27. TplActivate base.TplName = "user/auth/activate"
  28. tplForgotPassword base.TplName = "user/auth/forgot_passwd"
  29. tplResetPassword base.TplName = "user/auth/reset_passwd"
  30. tplTwofa base.TplName = "user/auth/twofa"
  31. tplTwofaScratch base.TplName = "user/auth/twofa_scratch"
  32. tplLinkAccount base.TplName = "user/auth/link_account"
  33. )
  34. // AutoSignIn reads cookie and try to auto-login.
  35. func AutoSignIn(ctx *context.Context) (bool, error) {
  36. if !models.HasEngine {
  37. return false, nil
  38. }
  39. uname := ctx.GetCookie(setting.CookieUserName)
  40. if len(uname) == 0 {
  41. return false, nil
  42. }
  43. isSucceed := false
  44. defer func() {
  45. if !isSucceed {
  46. log.Trace("auto-login cookie cleared: %s", uname)
  47. ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL)
  48. ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL)
  49. }
  50. }()
  51. u, err := models.GetUserByName(uname)
  52. if err != nil {
  53. if !models.IsErrUserNotExist(err) {
  54. return false, fmt.Errorf("GetUserByName: %v", err)
  55. }
  56. return false, nil
  57. }
  58. if val, _ := ctx.GetSuperSecureCookie(
  59. base.EncodeMD5(u.Rands+u.Passwd), setting.CookieRememberName); val != u.Name {
  60. return false, nil
  61. }
  62. isSucceed = true
  63. ctx.Session.Set("uid", u.ID)
  64. ctx.Session.Set("uname", u.Name)
  65. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL)
  66. return true, nil
  67. }
  68. func checkAutoLogin(ctx *context.Context) bool {
  69. // Check auto-login.
  70. isSucceed, err := AutoSignIn(ctx)
  71. if err != nil {
  72. ctx.ServerError("AutoSignIn", err)
  73. return true
  74. }
  75. redirectTo := ctx.Query("redirect_to")
  76. if len(redirectTo) > 0 {
  77. ctx.SetCookie("redirect_to", redirectTo, 0, setting.AppSubURL)
  78. } else {
  79. redirectTo, _ = url.QueryUnescape(ctx.GetCookie("redirect_to"))
  80. }
  81. if isSucceed {
  82. ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
  83. ctx.RedirectToFirst(redirectTo, setting.AppSubURL+string(setting.LandingPageURL))
  84. return true
  85. }
  86. return false
  87. }
  88. // SignIn render sign in page
  89. func SignIn(ctx *context.Context) {
  90. ctx.Data["Title"] = ctx.Tr("sign_in")
  91. // Check auto-login.
  92. if checkAutoLogin(ctx) {
  93. return
  94. }
  95. orderedOAuth2Names, oauth2Providers, err := models.GetActiveOAuth2Providers()
  96. if err != nil {
  97. ctx.ServerError("UserSignIn", err)
  98. return
  99. }
  100. ctx.Data["OrderedOAuth2Names"] = orderedOAuth2Names
  101. ctx.Data["OAuth2Providers"] = oauth2Providers
  102. ctx.Data["Title"] = ctx.Tr("sign_in")
  103. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login"
  104. ctx.Data["PageIsSignIn"] = true
  105. ctx.Data["PageIsLogin"] = true
  106. ctx.HTML(200, tplSignIn)
  107. }
  108. // SignInPost response for sign in request
  109. func SignInPost(ctx *context.Context, form auth.SignInForm) {
  110. ctx.Data["Title"] = ctx.Tr("sign_in")
  111. orderedOAuth2Names, oauth2Providers, err := models.GetActiveOAuth2Providers()
  112. if err != nil {
  113. ctx.ServerError("UserSignIn", err)
  114. return
  115. }
  116. ctx.Data["OrderedOAuth2Names"] = orderedOAuth2Names
  117. ctx.Data["OAuth2Providers"] = oauth2Providers
  118. ctx.Data["Title"] = ctx.Tr("sign_in")
  119. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login"
  120. ctx.Data["PageIsSignIn"] = true
  121. ctx.Data["PageIsLogin"] = true
  122. if ctx.HasError() {
  123. ctx.HTML(200, tplSignIn)
  124. return
  125. }
  126. u, err := models.UserSignIn(form.UserName, form.Password)
  127. if err != nil {
  128. if models.IsErrUserNotExist(err) {
  129. ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplSignIn, &form)
  130. log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr())
  131. } else if models.IsErrEmailAlreadyUsed(err) {
  132. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignIn, &form)
  133. log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr())
  134. } else {
  135. ctx.ServerError("UserSignIn", err)
  136. }
  137. return
  138. }
  139. // If this user is enrolled in 2FA, we can't sign the user in just yet.
  140. // Instead, redirect them to the 2FA authentication page.
  141. _, err = models.GetTwoFactorByUID(u.ID)
  142. if err != nil {
  143. if models.IsErrTwoFactorNotEnrolled(err) {
  144. handleSignIn(ctx, u, form.Remember)
  145. } else {
  146. ctx.ServerError("UserSignIn", err)
  147. }
  148. return
  149. }
  150. // User needs to use 2FA, save data and redirect to 2FA page.
  151. ctx.Session.Set("twofaUid", u.ID)
  152. ctx.Session.Set("twofaRemember", form.Remember)
  153. ctx.Redirect(setting.AppSubURL + "/user/two_factor")
  154. }
  155. // TwoFactor shows the user a two-factor authentication page.
  156. func TwoFactor(ctx *context.Context) {
  157. ctx.Data["Title"] = ctx.Tr("twofa")
  158. // Check auto-login.
  159. if checkAutoLogin(ctx) {
  160. return
  161. }
  162. // Ensure user is in a 2FA session.
  163. if ctx.Session.Get("twofaUid") == nil {
  164. ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
  165. return
  166. }
  167. ctx.HTML(200, tplTwofa)
  168. }
  169. // TwoFactorPost validates a user's two-factor authentication token.
  170. func TwoFactorPost(ctx *context.Context, form auth.TwoFactorAuthForm) {
  171. ctx.Data["Title"] = ctx.Tr("twofa")
  172. // Ensure user is in a 2FA session.
  173. idSess := ctx.Session.Get("twofaUid")
  174. if idSess == nil {
  175. ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
  176. return
  177. }
  178. id := idSess.(int64)
  179. twofa, err := models.GetTwoFactorByUID(id)
  180. if err != nil {
  181. ctx.ServerError("UserSignIn", err)
  182. return
  183. }
  184. // Validate the passcode with the stored TOTP secret.
  185. ok, err := twofa.ValidateTOTP(form.Passcode)
  186. if err != nil {
  187. ctx.ServerError("UserSignIn", err)
  188. return
  189. }
  190. if ok && twofa.LastUsedPasscode != form.Passcode {
  191. remember := ctx.Session.Get("twofaRemember").(bool)
  192. u, err := models.GetUserByID(id)
  193. if err != nil {
  194. ctx.ServerError("UserSignIn", err)
  195. return
  196. }
  197. if ctx.Session.Get("linkAccount") != nil {
  198. gothUser := ctx.Session.Get("linkAccountGothUser")
  199. if gothUser == nil {
  200. ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
  201. return
  202. }
  203. err = models.LinkAccountToUser(u, gothUser.(goth.User))
  204. if err != nil {
  205. ctx.ServerError("UserSignIn", err)
  206. return
  207. }
  208. }
  209. twofa.LastUsedPasscode = form.Passcode
  210. if err = models.UpdateTwoFactor(twofa); err != nil {
  211. ctx.ServerError("UserSignIn", err)
  212. return
  213. }
  214. handleSignIn(ctx, u, remember)
  215. return
  216. }
  217. ctx.RenderWithErr(ctx.Tr("auth.twofa_passcode_incorrect"), tplTwofa, auth.TwoFactorAuthForm{})
  218. }
  219. // TwoFactorScratch shows the scratch code form for two-factor authentication.
  220. func TwoFactorScratch(ctx *context.Context) {
  221. ctx.Data["Title"] = ctx.Tr("twofa_scratch")
  222. // Check auto-login.
  223. if checkAutoLogin(ctx) {
  224. return
  225. }
  226. // Ensure user is in a 2FA session.
  227. if ctx.Session.Get("twofaUid") == nil {
  228. ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
  229. return
  230. }
  231. ctx.HTML(200, tplTwofaScratch)
  232. }
  233. // TwoFactorScratchPost validates and invalidates a user's two-factor scratch token.
  234. func TwoFactorScratchPost(ctx *context.Context, form auth.TwoFactorScratchAuthForm) {
  235. ctx.Data["Title"] = ctx.Tr("twofa_scratch")
  236. // Ensure user is in a 2FA session.
  237. idSess := ctx.Session.Get("twofaUid")
  238. if idSess == nil {
  239. ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
  240. return
  241. }
  242. id := idSess.(int64)
  243. twofa, err := models.GetTwoFactorByUID(id)
  244. if err != nil {
  245. ctx.ServerError("UserSignIn", err)
  246. return
  247. }
  248. // Validate the passcode with the stored TOTP secret.
  249. if twofa.VerifyScratchToken(form.Token) {
  250. // Invalidate the scratch token.
  251. twofa.ScratchToken = ""
  252. if err = models.UpdateTwoFactor(twofa); err != nil {
  253. ctx.ServerError("UserSignIn", err)
  254. return
  255. }
  256. remember := ctx.Session.Get("twofaRemember").(bool)
  257. u, err := models.GetUserByID(id)
  258. if err != nil {
  259. ctx.ServerError("UserSignIn", err)
  260. return
  261. }
  262. handleSignInFull(ctx, u, remember, false)
  263. ctx.Flash.Info(ctx.Tr("auth.twofa_scratch_used"))
  264. ctx.Redirect(setting.AppSubURL + "/user/settings/two_factor")
  265. return
  266. }
  267. ctx.RenderWithErr(ctx.Tr("auth.twofa_scratch_token_incorrect"), tplTwofaScratch, auth.TwoFactorScratchAuthForm{})
  268. }
  269. // This handles the final part of the sign-in process of the user.
  270. func handleSignIn(ctx *context.Context, u *models.User, remember bool) {
  271. handleSignInFull(ctx, u, remember, true)
  272. }
  273. func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyRedirect bool) {
  274. if remember {
  275. days := 86400 * setting.LogInRememberDays
  276. ctx.SetCookie(setting.CookieUserName, u.Name, days, setting.AppSubURL)
  277. ctx.SetSuperSecureCookie(base.EncodeMD5(u.Rands+u.Passwd),
  278. setting.CookieRememberName, u.Name, days, setting.AppSubURL)
  279. }
  280. ctx.Session.Delete("openid_verified_uri")
  281. ctx.Session.Delete("openid_signin_remember")
  282. ctx.Session.Delete("openid_determined_email")
  283. ctx.Session.Delete("openid_determined_username")
  284. ctx.Session.Delete("twofaUid")
  285. ctx.Session.Delete("twofaRemember")
  286. ctx.Session.Set("uid", u.ID)
  287. ctx.Session.Set("uname", u.Name)
  288. // Language setting of the user overwrites the one previously set
  289. // If the user does not have a locale set, we save the current one.
  290. if len(u.Language) == 0 {
  291. u.Language = ctx.Locale.Language()
  292. if err := models.UpdateUserCols(u, "language"); err != nil {
  293. log.Error(4, fmt.Sprintf("Error updating user language [user: %d, locale: %s]", u.ID, u.Language))
  294. return
  295. }
  296. }
  297. ctx.SetCookie("lang", u.Language, nil, setting.AppSubURL)
  298. // Clear whatever CSRF has right now, force to generate a new one
  299. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL)
  300. // Register last login
  301. u.SetLastLogin()
  302. if err := models.UpdateUserCols(u, "last_login_unix"); err != nil {
  303. ctx.ServerError("UpdateUserCols", err)
  304. return
  305. }
  306. if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
  307. ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
  308. if obeyRedirect {
  309. ctx.RedirectToFirst(redirectTo)
  310. }
  311. return
  312. }
  313. if obeyRedirect {
  314. ctx.Redirect(setting.AppSubURL + "/")
  315. }
  316. }
  317. // SignInOAuth handles the OAuth2 login buttons
  318. func SignInOAuth(ctx *context.Context) {
  319. provider := ctx.Params(":provider")
  320. loginSource, err := models.GetActiveOAuth2LoginSourceByName(provider)
  321. if err != nil {
  322. ctx.ServerError("SignIn", err)
  323. return
  324. }
  325. // try to do a direct callback flow, so we don't authenticate the user again but use the valid accesstoken to get the user
  326. user, gothUser, err := oAuth2UserLoginCallback(loginSource, ctx.Req.Request, ctx.Resp)
  327. if err == nil && user != nil {
  328. // we got the user without going through the whole OAuth2 authentication flow again
  329. handleOAuth2SignIn(user, gothUser, ctx, err)
  330. return
  331. }
  332. err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp)
  333. if err != nil {
  334. ctx.ServerError("SignIn", err)
  335. }
  336. // redirect is done in oauth2.Auth
  337. }
  338. // SignInOAuthCallback handles the callback from the given provider
  339. func SignInOAuthCallback(ctx *context.Context) {
  340. provider := ctx.Params(":provider")
  341. // first look if the provider is still active
  342. loginSource, err := models.GetActiveOAuth2LoginSourceByName(provider)
  343. if err != nil {
  344. ctx.ServerError("SignIn", err)
  345. return
  346. }
  347. if loginSource == nil {
  348. ctx.ServerError("SignIn", errors.New("No valid provider found, check configured callback url in provider"))
  349. return
  350. }
  351. u, gothUser, err := oAuth2UserLoginCallback(loginSource, ctx.Req.Request, ctx.Resp)
  352. handleOAuth2SignIn(u, gothUser, ctx, err)
  353. }
  354. func handleOAuth2SignIn(u *models.User, gothUser goth.User, ctx *context.Context, err error) {
  355. if err != nil {
  356. ctx.ServerError("UserSignIn", err)
  357. return
  358. }
  359. if u == nil {
  360. // no existing user is found, request attach or new account
  361. ctx.Session.Set("linkAccountGothUser", gothUser)
  362. ctx.Redirect(setting.AppSubURL + "/user/link_account")
  363. return
  364. }
  365. // If this user is enrolled in 2FA, we can't sign the user in just yet.
  366. // Instead, redirect them to the 2FA authentication page.
  367. _, err = models.GetTwoFactorByUID(u.ID)
  368. if err != nil {
  369. if models.IsErrTwoFactorNotEnrolled(err) {
  370. ctx.Session.Set("uid", u.ID)
  371. ctx.Session.Set("uname", u.Name)
  372. // Clear whatever CSRF has right now, force to generate a new one
  373. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL)
  374. // Register last login
  375. u.SetLastLogin()
  376. if err := models.UpdateUserCols(u, "last_login_unix"); err != nil {
  377. ctx.ServerError("UpdateUserCols", err)
  378. return
  379. }
  380. if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
  381. ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
  382. ctx.RedirectToFirst(redirectTo)
  383. return
  384. }
  385. ctx.Redirect(setting.AppSubURL + "/")
  386. } else {
  387. ctx.ServerError("UserSignIn", err)
  388. }
  389. return
  390. }
  391. // User needs to use 2FA, save data and redirect to 2FA page.
  392. ctx.Session.Set("twofaUid", u.ID)
  393. ctx.Session.Set("twofaRemember", false)
  394. ctx.Redirect(setting.AppSubURL + "/user/two_factor")
  395. }
  396. // OAuth2UserLoginCallback attempts to handle the callback from the OAuth2 provider and if successful
  397. // login the user
  398. func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Request, response http.ResponseWriter) (*models.User, goth.User, error) {
  399. gothUser, err := oauth2.ProviderCallback(loginSource.Name, request, response)
  400. if err != nil {
  401. return nil, goth.User{}, err
  402. }
  403. user := &models.User{
  404. LoginName: gothUser.UserID,
  405. LoginType: models.LoginOAuth2,
  406. LoginSource: loginSource.ID,
  407. }
  408. hasUser, err := models.GetUser(user)
  409. if err != nil {
  410. return nil, goth.User{}, err
  411. }
  412. if hasUser {
  413. return user, goth.User{}, nil
  414. }
  415. // search in external linked users
  416. externalLoginUser := &models.ExternalLoginUser{
  417. ExternalID: gothUser.UserID,
  418. LoginSourceID: loginSource.ID,
  419. }
  420. hasUser, err = models.GetExternalLogin(externalLoginUser)
  421. if err != nil {
  422. return nil, goth.User{}, err
  423. }
  424. if hasUser {
  425. user, err = models.GetUserByID(externalLoginUser.UserID)
  426. return user, goth.User{}, err
  427. }
  428. // no user found to login
  429. return nil, gothUser, nil
  430. }
  431. // LinkAccount shows the page where the user can decide to login or create a new account
  432. func LinkAccount(ctx *context.Context) {
  433. ctx.Data["Title"] = ctx.Tr("link_account")
  434. ctx.Data["LinkAccountMode"] = true
  435. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  436. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  437. ctx.Data["ShowRegistrationButton"] = false
  438. // use this to set the right link into the signIn and signUp templates in the link_account template
  439. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
  440. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
  441. gothUser := ctx.Session.Get("linkAccountGothUser")
  442. if gothUser == nil {
  443. ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
  444. return
  445. }
  446. ctx.Data["user_name"] = gothUser.(goth.User).NickName
  447. ctx.Data["email"] = gothUser.(goth.User).Email
  448. ctx.HTML(200, tplLinkAccount)
  449. }
  450. // LinkAccountPostSignIn handle the coupling of external account with another account using signIn
  451. func LinkAccountPostSignIn(ctx *context.Context, signInForm auth.SignInForm) {
  452. ctx.Data["Title"] = ctx.Tr("link_account")
  453. ctx.Data["LinkAccountMode"] = true
  454. ctx.Data["LinkAccountModeSignIn"] = true
  455. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  456. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  457. ctx.Data["ShowRegistrationButton"] = false
  458. // use this to set the right link into the signIn and signUp templates in the link_account template
  459. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
  460. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
  461. gothUser := ctx.Session.Get("linkAccountGothUser")
  462. if gothUser == nil {
  463. ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
  464. return
  465. }
  466. if ctx.HasError() {
  467. ctx.HTML(200, tplLinkAccount)
  468. return
  469. }
  470. u, err := models.UserSignIn(signInForm.UserName, signInForm.Password)
  471. if err != nil {
  472. if models.IsErrUserNotExist(err) {
  473. ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplLinkAccount, &signInForm)
  474. } else {
  475. ctx.ServerError("UserLinkAccount", err)
  476. }
  477. return
  478. }
  479. // If this user is enrolled in 2FA, we can't sign the user in just yet.
  480. // Instead, redirect them to the 2FA authentication page.
  481. _, err = models.GetTwoFactorByUID(u.ID)
  482. if err != nil {
  483. if models.IsErrTwoFactorNotEnrolled(err) {
  484. err = models.LinkAccountToUser(u, gothUser.(goth.User))
  485. if err != nil {
  486. ctx.ServerError("UserLinkAccount", err)
  487. } else {
  488. handleSignIn(ctx, u, signInForm.Remember)
  489. }
  490. } else {
  491. ctx.ServerError("UserLinkAccount", err)
  492. }
  493. return
  494. }
  495. // User needs to use 2FA, save data and redirect to 2FA page.
  496. ctx.Session.Set("twofaUid", u.ID)
  497. ctx.Session.Set("twofaRemember", signInForm.Remember)
  498. ctx.Session.Set("linkAccount", true)
  499. ctx.Redirect(setting.AppSubURL + "/user/two_factor")
  500. }
  501. // LinkAccountPostRegister handle the creation of a new account for an external account using signUp
  502. func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
  503. ctx.Data["Title"] = ctx.Tr("link_account")
  504. ctx.Data["LinkAccountMode"] = true
  505. ctx.Data["LinkAccountModeRegister"] = true
  506. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  507. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  508. ctx.Data["ShowRegistrationButton"] = false
  509. // use this to set the right link into the signIn and signUp templates in the link_account template
  510. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
  511. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
  512. gothUser := ctx.Session.Get("linkAccountGothUser")
  513. if gothUser == nil {
  514. ctx.ServerError("UserSignUp", errors.New("not in LinkAccount session"))
  515. return
  516. }
  517. if ctx.HasError() {
  518. ctx.HTML(200, tplLinkAccount)
  519. return
  520. }
  521. if setting.Service.DisableRegistration {
  522. ctx.Error(403)
  523. return
  524. }
  525. if setting.Service.EnableCaptcha && !cpt.VerifyReq(ctx.Req) {
  526. ctx.Data["Err_Captcha"] = true
  527. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplLinkAccount, &form)
  528. return
  529. }
  530. if (len(strings.TrimSpace(form.Password)) > 0 || len(strings.TrimSpace(form.Retype)) > 0) && form.Password != form.Retype {
  531. ctx.Data["Err_Password"] = true
  532. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplLinkAccount, &form)
  533. return
  534. }
  535. if len(strings.TrimSpace(form.Password)) > 0 && len(form.Password) < setting.MinPasswordLength {
  536. ctx.Data["Err_Password"] = true
  537. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplLinkAccount, &form)
  538. return
  539. }
  540. loginSource, err := models.GetActiveOAuth2LoginSourceByName(gothUser.(goth.User).Provider)
  541. if err != nil {
  542. ctx.ServerError("CreateUser", err)
  543. }
  544. u := &models.User{
  545. Name: form.UserName,
  546. Email: form.Email,
  547. Passwd: form.Password,
  548. IsActive: !setting.Service.RegisterEmailConfirm,
  549. LoginType: models.LoginOAuth2,
  550. LoginSource: loginSource.ID,
  551. LoginName: gothUser.(goth.User).UserID,
  552. }
  553. if err := models.CreateUser(u); err != nil {
  554. switch {
  555. case models.IsErrUserAlreadyExist(err):
  556. ctx.Data["Err_UserName"] = true
  557. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplLinkAccount, &form)
  558. case models.IsErrEmailAlreadyUsed(err):
  559. ctx.Data["Err_Email"] = true
  560. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplLinkAccount, &form)
  561. case models.IsErrNameReserved(err):
  562. ctx.Data["Err_UserName"] = true
  563. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplLinkAccount, &form)
  564. case models.IsErrNamePatternNotAllowed(err):
  565. ctx.Data["Err_UserName"] = true
  566. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplLinkAccount, &form)
  567. default:
  568. ctx.ServerError("CreateUser", err)
  569. }
  570. return
  571. }
  572. log.Trace("Account created: %s", u.Name)
  573. // Auto-set admin for the only user.
  574. if models.CountUsers() == 1 {
  575. u.IsAdmin = true
  576. u.IsActive = true
  577. u.SetLastLogin()
  578. if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
  579. ctx.ServerError("UpdateUser", err)
  580. return
  581. }
  582. }
  583. // Send confirmation email
  584. if setting.Service.RegisterEmailConfirm && u.ID > 1 {
  585. models.SendActivateAccountMail(ctx.Context, u)
  586. ctx.Data["IsSendRegisterMail"] = true
  587. ctx.Data["Email"] = u.Email
  588. ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  589. ctx.HTML(200, TplActivate)
  590. if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  591. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  592. }
  593. return
  594. }
  595. ctx.Redirect(setting.AppSubURL + "/user/login")
  596. }
  597. // SignOut sign out from login status
  598. func SignOut(ctx *context.Context) {
  599. ctx.Session.Delete("uid")
  600. ctx.Session.Delete("uname")
  601. ctx.Session.Delete("socialId")
  602. ctx.Session.Delete("socialName")
  603. ctx.Session.Delete("socialEmail")
  604. ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL)
  605. ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL)
  606. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL)
  607. ctx.SetCookie("lang", "", -1, setting.AppSubURL) // Setting the lang cookie will trigger the middleware to reset the language ot previous state.
  608. ctx.Redirect(setting.AppSubURL + "/")
  609. }
  610. // SignUp render the register page
  611. func SignUp(ctx *context.Context) {
  612. ctx.Data["Title"] = ctx.Tr("sign_up")
  613. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
  614. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  615. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  616. ctx.HTML(200, tplSignUp)
  617. }
  618. // SignUpPost response for sign up information submission
  619. func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
  620. ctx.Data["Title"] = ctx.Tr("sign_up")
  621. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
  622. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  623. if setting.Service.DisableRegistration {
  624. ctx.Error(403)
  625. return
  626. }
  627. if ctx.HasError() {
  628. ctx.HTML(200, tplSignUp)
  629. return
  630. }
  631. if setting.Service.EnableCaptcha && !cpt.VerifyReq(ctx.Req) {
  632. ctx.Data["Err_Captcha"] = true
  633. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplSignUp, &form)
  634. return
  635. }
  636. if form.Password != form.Retype {
  637. ctx.Data["Err_Password"] = true
  638. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplSignUp, &form)
  639. return
  640. }
  641. if len(form.Password) < setting.MinPasswordLength {
  642. ctx.Data["Err_Password"] = true
  643. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplSignUp, &form)
  644. return
  645. }
  646. u := &models.User{
  647. Name: form.UserName,
  648. Email: form.Email,
  649. Passwd: form.Password,
  650. IsActive: !setting.Service.RegisterEmailConfirm,
  651. }
  652. if err := models.CreateUser(u); err != nil {
  653. switch {
  654. case models.IsErrUserAlreadyExist(err):
  655. ctx.Data["Err_UserName"] = true
  656. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplSignUp, &form)
  657. case models.IsErrEmailAlreadyUsed(err):
  658. ctx.Data["Err_Email"] = true
  659. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignUp, &form)
  660. case models.IsErrNameReserved(err):
  661. ctx.Data["Err_UserName"] = true
  662. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplSignUp, &form)
  663. case models.IsErrNamePatternNotAllowed(err):
  664. ctx.Data["Err_UserName"] = true
  665. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplSignUp, &form)
  666. default:
  667. ctx.ServerError("CreateUser", err)
  668. }
  669. return
  670. }
  671. log.Trace("Account created: %s", u.Name)
  672. // Auto-set admin for the only user.
  673. if models.CountUsers() == 1 {
  674. u.IsAdmin = true
  675. u.IsActive = true
  676. u.SetLastLogin()
  677. if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
  678. ctx.ServerError("UpdateUser", err)
  679. return
  680. }
  681. }
  682. // Send confirmation email, no need for social account.
  683. if setting.Service.RegisterEmailConfirm && u.ID > 1 {
  684. models.SendActivateAccountMail(ctx.Context, u)
  685. ctx.Data["IsSendRegisterMail"] = true
  686. ctx.Data["Email"] = u.Email
  687. ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  688. ctx.HTML(200, TplActivate)
  689. if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  690. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  691. }
  692. return
  693. }
  694. ctx.Redirect(setting.AppSubURL + "/user/login")
  695. }
  696. // Activate render activate user page
  697. func Activate(ctx *context.Context) {
  698. code := ctx.Query("code")
  699. if len(code) == 0 {
  700. ctx.Data["IsActivatePage"] = true
  701. if ctx.User.IsActive {
  702. ctx.Error(404)
  703. return
  704. }
  705. // Resend confirmation email.
  706. if setting.Service.RegisterEmailConfirm {
  707. if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
  708. ctx.Data["ResendLimited"] = true
  709. } else {
  710. ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  711. models.SendActivateAccountMail(ctx.Context, ctx.User)
  712. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  713. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  714. }
  715. }
  716. } else {
  717. ctx.Data["ServiceNotEnabled"] = true
  718. }
  719. ctx.HTML(200, TplActivate)
  720. return
  721. }
  722. // Verify code.
  723. if user := models.VerifyUserActiveCode(code); user != nil {
  724. user.IsActive = true
  725. var err error
  726. if user.Rands, err = models.GetUserSalt(); err != nil {
  727. ctx.ServerError("UpdateUser", err)
  728. return
  729. }
  730. if err := models.UpdateUserCols(user, "is_active", "rands"); err != nil {
  731. if models.IsErrUserNotExist(err) {
  732. ctx.Error(404)
  733. } else {
  734. ctx.ServerError("UpdateUser", err)
  735. }
  736. return
  737. }
  738. log.Trace("User activated: %s", user.Name)
  739. ctx.Session.Set("uid", user.ID)
  740. ctx.Session.Set("uname", user.Name)
  741. ctx.Redirect(setting.AppSubURL + "/")
  742. return
  743. }
  744. ctx.Data["IsActivateFailed"] = true
  745. ctx.HTML(200, TplActivate)
  746. }
  747. // ActivateEmail render the activate email page
  748. func ActivateEmail(ctx *context.Context) {
  749. code := ctx.Query("code")
  750. emailStr := ctx.Query("email")
  751. // Verify code.
  752. if email := models.VerifyActiveEmailCode(code, emailStr); email != nil {
  753. if err := email.Activate(); err != nil {
  754. ctx.ServerError("ActivateEmail", err)
  755. }
  756. log.Trace("Email activated: %s", email.Email)
  757. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  758. }
  759. ctx.Redirect(setting.AppSubURL + "/user/settings/email")
  760. return
  761. }
  762. // ForgotPasswd render the forget pasword page
  763. func ForgotPasswd(ctx *context.Context) {
  764. ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title")
  765. if setting.MailService == nil {
  766. ctx.Data["IsResetDisable"] = true
  767. ctx.HTML(200, tplForgotPassword)
  768. return
  769. }
  770. email := ctx.Query("email")
  771. ctx.Data["Email"] = email
  772. ctx.Data["IsResetRequest"] = true
  773. ctx.HTML(200, tplForgotPassword)
  774. }
  775. // ForgotPasswdPost response for forget password request
  776. func ForgotPasswdPost(ctx *context.Context) {
  777. ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title")
  778. if setting.MailService == nil {
  779. ctx.NotFound("ForgotPasswdPost", nil)
  780. return
  781. }
  782. ctx.Data["IsResetRequest"] = true
  783. email := ctx.Query("email")
  784. ctx.Data["Email"] = email
  785. u, err := models.GetUserByEmail(email)
  786. if err != nil {
  787. if models.IsErrUserNotExist(err) {
  788. ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
  789. ctx.Data["IsResetSent"] = true
  790. ctx.HTML(200, tplForgotPassword)
  791. return
  792. }
  793. ctx.ServerError("user.ResetPasswd(check existence)", err)
  794. return
  795. }
  796. if !u.IsLocal() && !u.IsOAuth2() {
  797. ctx.Data["Err_Email"] = true
  798. ctx.RenderWithErr(ctx.Tr("auth.non_local_account"), tplForgotPassword, nil)
  799. return
  800. }
  801. if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) {
  802. ctx.Data["ResendLimited"] = true
  803. ctx.HTML(200, tplForgotPassword)
  804. return
  805. }
  806. models.SendResetPasswordMail(ctx.Context, u)
  807. if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  808. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  809. }
  810. ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
  811. ctx.Data["IsResetSent"] = true
  812. ctx.HTML(200, tplForgotPassword)
  813. }
  814. // ResetPasswd render the reset password page
  815. func ResetPasswd(ctx *context.Context) {
  816. ctx.Data["Title"] = ctx.Tr("auth.reset_password")
  817. code := ctx.Query("code")
  818. if len(code) == 0 {
  819. ctx.Error(404)
  820. return
  821. }
  822. ctx.Data["Code"] = code
  823. ctx.Data["IsResetForm"] = true
  824. ctx.HTML(200, tplResetPassword)
  825. }
  826. // ResetPasswdPost response from reset password request
  827. func ResetPasswdPost(ctx *context.Context) {
  828. ctx.Data["Title"] = ctx.Tr("auth.reset_password")
  829. code := ctx.Query("code")
  830. if len(code) == 0 {
  831. ctx.Error(404)
  832. return
  833. }
  834. ctx.Data["Code"] = code
  835. if u := models.VerifyUserActiveCode(code); u != nil {
  836. // Validate password length.
  837. passwd := ctx.Query("password")
  838. if len(passwd) < setting.MinPasswordLength {
  839. ctx.Data["IsResetForm"] = true
  840. ctx.Data["Err_Password"] = true
  841. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplResetPassword, nil)
  842. return
  843. }
  844. var err error
  845. if u.Rands, err = models.GetUserSalt(); err != nil {
  846. ctx.ServerError("UpdateUser", err)
  847. return
  848. }
  849. if u.Salt, err = models.GetUserSalt(); err != nil {
  850. ctx.ServerError("UpdateUser", err)
  851. return
  852. }
  853. u.HashPassword(passwd)
  854. if err := models.UpdateUserCols(u, "passwd", "rands", "salt"); err != nil {
  855. ctx.ServerError("UpdateUser", err)
  856. return
  857. }
  858. log.Trace("User password reset: %s", u.Name)
  859. ctx.Redirect(setting.AppSubURL + "/user/login")
  860. return
  861. }
  862. ctx.Data["IsResetFailed"] = true
  863. ctx.HTML(200, tplResetPassword)
  864. }