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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 {
  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. handleSignIn(ctx, u, remember)
  210. return
  211. }
  212. ctx.RenderWithErr(ctx.Tr("auth.twofa_passcode_incorrect"), tplTwofa, auth.TwoFactorAuthForm{})
  213. }
  214. // TwoFactorScratch shows the scratch code form for two-factor authentication.
  215. func TwoFactorScratch(ctx *context.Context) {
  216. ctx.Data["Title"] = ctx.Tr("twofa_scratch")
  217. // Check auto-login.
  218. if checkAutoLogin(ctx) {
  219. return
  220. }
  221. // Ensure user is in a 2FA session.
  222. if ctx.Session.Get("twofaUid") == nil {
  223. ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
  224. return
  225. }
  226. ctx.HTML(200, tplTwofaScratch)
  227. }
  228. // TwoFactorScratchPost validates and invalidates a user's two-factor scratch token.
  229. func TwoFactorScratchPost(ctx *context.Context, form auth.TwoFactorScratchAuthForm) {
  230. ctx.Data["Title"] = ctx.Tr("twofa_scratch")
  231. // Ensure user is in a 2FA session.
  232. idSess := ctx.Session.Get("twofaUid")
  233. if idSess == nil {
  234. ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
  235. return
  236. }
  237. id := idSess.(int64)
  238. twofa, err := models.GetTwoFactorByUID(id)
  239. if err != nil {
  240. ctx.ServerError("UserSignIn", err)
  241. return
  242. }
  243. // Validate the passcode with the stored TOTP secret.
  244. if twofa.VerifyScratchToken(form.Token) {
  245. // Invalidate the scratch token.
  246. twofa.ScratchToken = ""
  247. if err = models.UpdateTwoFactor(twofa); err != nil {
  248. ctx.ServerError("UserSignIn", err)
  249. return
  250. }
  251. remember := ctx.Session.Get("twofaRemember").(bool)
  252. u, err := models.GetUserByID(id)
  253. if err != nil {
  254. ctx.ServerError("UserSignIn", err)
  255. return
  256. }
  257. handleSignInFull(ctx, u, remember, false)
  258. ctx.Flash.Info(ctx.Tr("auth.twofa_scratch_used"))
  259. ctx.Redirect(setting.AppSubURL + "/user/settings/two_factor")
  260. return
  261. }
  262. ctx.RenderWithErr(ctx.Tr("auth.twofa_scratch_token_incorrect"), tplTwofaScratch, auth.TwoFactorScratchAuthForm{})
  263. }
  264. // This handles the final part of the sign-in process of the user.
  265. func handleSignIn(ctx *context.Context, u *models.User, remember bool) {
  266. handleSignInFull(ctx, u, remember, true)
  267. }
  268. func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyRedirect bool) {
  269. if remember {
  270. days := 86400 * setting.LogInRememberDays
  271. ctx.SetCookie(setting.CookieUserName, u.Name, days, setting.AppSubURL)
  272. ctx.SetSuperSecureCookie(base.EncodeMD5(u.Rands+u.Passwd),
  273. setting.CookieRememberName, u.Name, days, setting.AppSubURL)
  274. }
  275. ctx.Session.Delete("openid_verified_uri")
  276. ctx.Session.Delete("openid_signin_remember")
  277. ctx.Session.Delete("openid_determined_email")
  278. ctx.Session.Delete("openid_determined_username")
  279. ctx.Session.Delete("twofaUid")
  280. ctx.Session.Delete("twofaRemember")
  281. ctx.Session.Set("uid", u.ID)
  282. ctx.Session.Set("uname", u.Name)
  283. // Clear whatever CSRF has right now, force to generate a new one
  284. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL)
  285. // Register last login
  286. u.SetLastLogin()
  287. if err := models.UpdateUserCols(u, "last_login_unix"); err != nil {
  288. ctx.ServerError("UpdateUserCols", err)
  289. return
  290. }
  291. if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
  292. ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
  293. if obeyRedirect {
  294. ctx.RedirectToFirst(redirectTo)
  295. }
  296. return
  297. }
  298. if obeyRedirect {
  299. ctx.Redirect(setting.AppSubURL + "/")
  300. }
  301. }
  302. // SignInOAuth handles the OAuth2 login buttons
  303. func SignInOAuth(ctx *context.Context) {
  304. provider := ctx.Params(":provider")
  305. loginSource, err := models.GetActiveOAuth2LoginSourceByName(provider)
  306. if err != nil {
  307. ctx.ServerError("SignIn", err)
  308. return
  309. }
  310. // try to do a direct callback flow, so we don't authenticate the user again but use the valid accesstoken to get the user
  311. user, gothUser, err := oAuth2UserLoginCallback(loginSource, ctx.Req.Request, ctx.Resp)
  312. if err == nil && user != nil {
  313. // we got the user without going through the whole OAuth2 authentication flow again
  314. handleOAuth2SignIn(user, gothUser, ctx, err)
  315. return
  316. }
  317. err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp)
  318. if err != nil {
  319. ctx.ServerError("SignIn", err)
  320. }
  321. // redirect is done in oauth2.Auth
  322. }
  323. // SignInOAuthCallback handles the callback from the given provider
  324. func SignInOAuthCallback(ctx *context.Context) {
  325. provider := ctx.Params(":provider")
  326. // first look if the provider is still active
  327. loginSource, err := models.GetActiveOAuth2LoginSourceByName(provider)
  328. if err != nil {
  329. ctx.ServerError("SignIn", err)
  330. return
  331. }
  332. if loginSource == nil {
  333. ctx.ServerError("SignIn", errors.New("No valid provider found, check configured callback url in provider"))
  334. return
  335. }
  336. u, gothUser, err := oAuth2UserLoginCallback(loginSource, ctx.Req.Request, ctx.Resp)
  337. handleOAuth2SignIn(u, gothUser, ctx, err)
  338. }
  339. func handleOAuth2SignIn(u *models.User, gothUser goth.User, ctx *context.Context, err error) {
  340. if err != nil {
  341. ctx.ServerError("UserSignIn", err)
  342. return
  343. }
  344. if u == nil {
  345. // no existing user is found, request attach or new account
  346. ctx.Session.Set("linkAccountGothUser", gothUser)
  347. ctx.Redirect(setting.AppSubURL + "/user/link_account")
  348. return
  349. }
  350. // If this user is enrolled in 2FA, we can't sign the user in just yet.
  351. // Instead, redirect them to the 2FA authentication page.
  352. _, err = models.GetTwoFactorByUID(u.ID)
  353. if err != nil {
  354. if models.IsErrTwoFactorNotEnrolled(err) {
  355. ctx.Session.Set("uid", u.ID)
  356. ctx.Session.Set("uname", u.Name)
  357. // Clear whatever CSRF has right now, force to generate a new one
  358. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL)
  359. // Register last login
  360. u.SetLastLogin()
  361. if err := models.UpdateUserCols(u, "last_login_unix"); err != nil {
  362. ctx.ServerError("UpdateUserCols", err)
  363. return
  364. }
  365. if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
  366. ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
  367. ctx.RedirectToFirst(redirectTo)
  368. return
  369. }
  370. ctx.Redirect(setting.AppSubURL + "/")
  371. } else {
  372. ctx.ServerError("UserSignIn", err)
  373. }
  374. return
  375. }
  376. // User needs to use 2FA, save data and redirect to 2FA page.
  377. ctx.Session.Set("twofaUid", u.ID)
  378. ctx.Session.Set("twofaRemember", false)
  379. ctx.Redirect(setting.AppSubURL + "/user/two_factor")
  380. }
  381. // OAuth2UserLoginCallback attempts to handle the callback from the OAuth2 provider and if successful
  382. // login the user
  383. func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Request, response http.ResponseWriter) (*models.User, goth.User, error) {
  384. gothUser, err := oauth2.ProviderCallback(loginSource.Name, request, response)
  385. if err != nil {
  386. return nil, goth.User{}, err
  387. }
  388. user := &models.User{
  389. LoginName: gothUser.UserID,
  390. LoginType: models.LoginOAuth2,
  391. LoginSource: loginSource.ID,
  392. }
  393. hasUser, err := models.GetUser(user)
  394. if err != nil {
  395. return nil, goth.User{}, err
  396. }
  397. if hasUser {
  398. return user, goth.User{}, nil
  399. }
  400. // search in external linked users
  401. externalLoginUser := &models.ExternalLoginUser{
  402. ExternalID: gothUser.UserID,
  403. LoginSourceID: loginSource.ID,
  404. }
  405. hasUser, err = models.GetExternalLogin(externalLoginUser)
  406. if err != nil {
  407. return nil, goth.User{}, err
  408. }
  409. if hasUser {
  410. user, err = models.GetUserByID(externalLoginUser.UserID)
  411. return user, goth.User{}, err
  412. }
  413. // no user found to login
  414. return nil, gothUser, nil
  415. }
  416. // LinkAccount shows the page where the user can decide to login or create a new account
  417. func LinkAccount(ctx *context.Context) {
  418. ctx.Data["Title"] = ctx.Tr("link_account")
  419. ctx.Data["LinkAccountMode"] = true
  420. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  421. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  422. ctx.Data["ShowRegistrationButton"] = false
  423. // use this to set the right link into the signIn and signUp templates in the link_account template
  424. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
  425. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
  426. gothUser := ctx.Session.Get("linkAccountGothUser")
  427. if gothUser == nil {
  428. ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
  429. return
  430. }
  431. ctx.Data["user_name"] = gothUser.(goth.User).NickName
  432. ctx.Data["email"] = gothUser.(goth.User).Email
  433. ctx.HTML(200, tplLinkAccount)
  434. }
  435. // LinkAccountPostSignIn handle the coupling of external account with another account using signIn
  436. func LinkAccountPostSignIn(ctx *context.Context, signInForm auth.SignInForm) {
  437. ctx.Data["Title"] = ctx.Tr("link_account")
  438. ctx.Data["LinkAccountMode"] = true
  439. ctx.Data["LinkAccountModeSignIn"] = true
  440. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  441. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  442. ctx.Data["ShowRegistrationButton"] = false
  443. // use this to set the right link into the signIn and signUp templates in the link_account template
  444. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
  445. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
  446. gothUser := ctx.Session.Get("linkAccountGothUser")
  447. if gothUser == nil {
  448. ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
  449. return
  450. }
  451. if ctx.HasError() {
  452. ctx.HTML(200, tplLinkAccount)
  453. return
  454. }
  455. u, err := models.UserSignIn(signInForm.UserName, signInForm.Password)
  456. if err != nil {
  457. if models.IsErrUserNotExist(err) {
  458. ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplLinkAccount, &signInForm)
  459. } else {
  460. ctx.ServerError("UserLinkAccount", err)
  461. }
  462. return
  463. }
  464. // If this user is enrolled in 2FA, we can't sign the user in just yet.
  465. // Instead, redirect them to the 2FA authentication page.
  466. _, err = models.GetTwoFactorByUID(u.ID)
  467. if err != nil {
  468. if models.IsErrTwoFactorNotEnrolled(err) {
  469. err = models.LinkAccountToUser(u, gothUser.(goth.User))
  470. if err != nil {
  471. ctx.ServerError("UserLinkAccount", err)
  472. } else {
  473. handleSignIn(ctx, u, signInForm.Remember)
  474. }
  475. } else {
  476. ctx.ServerError("UserLinkAccount", err)
  477. }
  478. return
  479. }
  480. // User needs to use 2FA, save data and redirect to 2FA page.
  481. ctx.Session.Set("twofaUid", u.ID)
  482. ctx.Session.Set("twofaRemember", signInForm.Remember)
  483. ctx.Session.Set("linkAccount", true)
  484. ctx.Redirect(setting.AppSubURL + "/user/two_factor")
  485. }
  486. // LinkAccountPostRegister handle the creation of a new account for an external account using signUp
  487. func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
  488. ctx.Data["Title"] = ctx.Tr("link_account")
  489. ctx.Data["LinkAccountMode"] = true
  490. ctx.Data["LinkAccountModeRegister"] = true
  491. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  492. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  493. ctx.Data["ShowRegistrationButton"] = false
  494. // use this to set the right link into the signIn and signUp templates in the link_account template
  495. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
  496. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
  497. gothUser := ctx.Session.Get("linkAccountGothUser")
  498. if gothUser == nil {
  499. ctx.ServerError("UserSignUp", errors.New("not in LinkAccount session"))
  500. return
  501. }
  502. if ctx.HasError() {
  503. ctx.HTML(200, tplLinkAccount)
  504. return
  505. }
  506. if setting.Service.DisableRegistration {
  507. ctx.Error(403)
  508. return
  509. }
  510. if setting.Service.EnableCaptcha && !cpt.VerifyReq(ctx.Req) {
  511. ctx.Data["Err_Captcha"] = true
  512. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplLinkAccount, &form)
  513. return
  514. }
  515. if (len(strings.TrimSpace(form.Password)) > 0 || len(strings.TrimSpace(form.Retype)) > 0) && form.Password != form.Retype {
  516. ctx.Data["Err_Password"] = true
  517. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplLinkAccount, &form)
  518. return
  519. }
  520. if len(strings.TrimSpace(form.Password)) > 0 && len(form.Password) < setting.MinPasswordLength {
  521. ctx.Data["Err_Password"] = true
  522. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplLinkAccount, &form)
  523. return
  524. }
  525. loginSource, err := models.GetActiveOAuth2LoginSourceByName(gothUser.(goth.User).Provider)
  526. if err != nil {
  527. ctx.ServerError("CreateUser", err)
  528. }
  529. u := &models.User{
  530. Name: form.UserName,
  531. Email: form.Email,
  532. Passwd: form.Password,
  533. IsActive: !setting.Service.RegisterEmailConfirm,
  534. LoginType: models.LoginOAuth2,
  535. LoginSource: loginSource.ID,
  536. LoginName: gothUser.(goth.User).UserID,
  537. }
  538. if err := models.CreateUser(u); err != nil {
  539. switch {
  540. case models.IsErrUserAlreadyExist(err):
  541. ctx.Data["Err_UserName"] = true
  542. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplLinkAccount, &form)
  543. case models.IsErrEmailAlreadyUsed(err):
  544. ctx.Data["Err_Email"] = true
  545. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplLinkAccount, &form)
  546. case models.IsErrNameReserved(err):
  547. ctx.Data["Err_UserName"] = true
  548. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplLinkAccount, &form)
  549. case models.IsErrNamePatternNotAllowed(err):
  550. ctx.Data["Err_UserName"] = true
  551. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplLinkAccount, &form)
  552. default:
  553. ctx.ServerError("CreateUser", err)
  554. }
  555. return
  556. }
  557. log.Trace("Account created: %s", u.Name)
  558. // Auto-set admin for the only user.
  559. if models.CountUsers() == 1 {
  560. u.IsAdmin = true
  561. u.IsActive = true
  562. u.SetLastLogin()
  563. if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
  564. ctx.ServerError("UpdateUser", err)
  565. return
  566. }
  567. }
  568. // Send confirmation email
  569. if setting.Service.RegisterEmailConfirm && u.ID > 1 {
  570. models.SendActivateAccountMail(ctx.Context, u)
  571. ctx.Data["IsSendRegisterMail"] = true
  572. ctx.Data["Email"] = u.Email
  573. ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  574. ctx.HTML(200, TplActivate)
  575. if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  576. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  577. }
  578. return
  579. }
  580. ctx.Redirect(setting.AppSubURL + "/user/login")
  581. }
  582. // SignOut sign out from login status
  583. func SignOut(ctx *context.Context) {
  584. ctx.Session.Delete("uid")
  585. ctx.Session.Delete("uname")
  586. ctx.Session.Delete("socialId")
  587. ctx.Session.Delete("socialName")
  588. ctx.Session.Delete("socialEmail")
  589. ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL)
  590. ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL)
  591. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL)
  592. ctx.Redirect(setting.AppSubURL + "/")
  593. }
  594. // SignUp render the register page
  595. func SignUp(ctx *context.Context) {
  596. ctx.Data["Title"] = ctx.Tr("sign_up")
  597. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
  598. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  599. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  600. ctx.HTML(200, tplSignUp)
  601. }
  602. // SignUpPost response for sign up information submission
  603. func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
  604. ctx.Data["Title"] = ctx.Tr("sign_up")
  605. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
  606. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  607. if setting.Service.DisableRegistration {
  608. ctx.Error(403)
  609. return
  610. }
  611. if ctx.HasError() {
  612. ctx.HTML(200, tplSignUp)
  613. return
  614. }
  615. if setting.Service.EnableCaptcha && !cpt.VerifyReq(ctx.Req) {
  616. ctx.Data["Err_Captcha"] = true
  617. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplSignUp, &form)
  618. return
  619. }
  620. if form.Password != form.Retype {
  621. ctx.Data["Err_Password"] = true
  622. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplSignUp, &form)
  623. return
  624. }
  625. if len(form.Password) < setting.MinPasswordLength {
  626. ctx.Data["Err_Password"] = true
  627. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplSignUp, &form)
  628. return
  629. }
  630. u := &models.User{
  631. Name: form.UserName,
  632. Email: form.Email,
  633. Passwd: form.Password,
  634. IsActive: !setting.Service.RegisterEmailConfirm,
  635. }
  636. if err := models.CreateUser(u); err != nil {
  637. switch {
  638. case models.IsErrUserAlreadyExist(err):
  639. ctx.Data["Err_UserName"] = true
  640. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplSignUp, &form)
  641. case models.IsErrEmailAlreadyUsed(err):
  642. ctx.Data["Err_Email"] = true
  643. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignUp, &form)
  644. case models.IsErrNameReserved(err):
  645. ctx.Data["Err_UserName"] = true
  646. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplSignUp, &form)
  647. case models.IsErrNamePatternNotAllowed(err):
  648. ctx.Data["Err_UserName"] = true
  649. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplSignUp, &form)
  650. default:
  651. ctx.ServerError("CreateUser", err)
  652. }
  653. return
  654. }
  655. log.Trace("Account created: %s", u.Name)
  656. // Auto-set admin for the only user.
  657. if models.CountUsers() == 1 {
  658. u.IsAdmin = true
  659. u.IsActive = true
  660. u.SetLastLogin()
  661. if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
  662. ctx.ServerError("UpdateUser", err)
  663. return
  664. }
  665. }
  666. // Send confirmation email, no need for social account.
  667. if setting.Service.RegisterEmailConfirm && u.ID > 1 {
  668. models.SendActivateAccountMail(ctx.Context, u)
  669. ctx.Data["IsSendRegisterMail"] = true
  670. ctx.Data["Email"] = u.Email
  671. ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  672. ctx.HTML(200, TplActivate)
  673. if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  674. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  675. }
  676. return
  677. }
  678. ctx.Redirect(setting.AppSubURL + "/user/login")
  679. }
  680. // Activate render activate user page
  681. func Activate(ctx *context.Context) {
  682. code := ctx.Query("code")
  683. if len(code) == 0 {
  684. ctx.Data["IsActivatePage"] = true
  685. if ctx.User.IsActive {
  686. ctx.Error(404)
  687. return
  688. }
  689. // Resend confirmation email.
  690. if setting.Service.RegisterEmailConfirm {
  691. if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
  692. ctx.Data["ResendLimited"] = true
  693. } else {
  694. ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  695. models.SendActivateAccountMail(ctx.Context, ctx.User)
  696. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  697. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  698. }
  699. }
  700. } else {
  701. ctx.Data["ServiceNotEnabled"] = true
  702. }
  703. ctx.HTML(200, TplActivate)
  704. return
  705. }
  706. // Verify code.
  707. if user := models.VerifyUserActiveCode(code); user != nil {
  708. user.IsActive = true
  709. var err error
  710. if user.Rands, err = models.GetUserSalt(); err != nil {
  711. ctx.ServerError("UpdateUser", err)
  712. return
  713. }
  714. if err := models.UpdateUserCols(user, "is_active", "rands"); err != nil {
  715. if models.IsErrUserNotExist(err) {
  716. ctx.Error(404)
  717. } else {
  718. ctx.ServerError("UpdateUser", err)
  719. }
  720. return
  721. }
  722. log.Trace("User activated: %s", user.Name)
  723. ctx.Session.Set("uid", user.ID)
  724. ctx.Session.Set("uname", user.Name)
  725. ctx.Redirect(setting.AppSubURL + "/")
  726. return
  727. }
  728. ctx.Data["IsActivateFailed"] = true
  729. ctx.HTML(200, TplActivate)
  730. }
  731. // ActivateEmail render the activate email page
  732. func ActivateEmail(ctx *context.Context) {
  733. code := ctx.Query("code")
  734. emailStr := ctx.Query("email")
  735. // Verify code.
  736. if email := models.VerifyActiveEmailCode(code, emailStr); email != nil {
  737. if err := email.Activate(); err != nil {
  738. ctx.ServerError("ActivateEmail", err)
  739. }
  740. log.Trace("Email activated: %s", email.Email)
  741. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  742. }
  743. ctx.Redirect(setting.AppSubURL + "/user/settings/email")
  744. return
  745. }
  746. // ForgotPasswd render the forget pasword page
  747. func ForgotPasswd(ctx *context.Context) {
  748. ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title")
  749. if setting.MailService == nil {
  750. ctx.Data["IsResetDisable"] = true
  751. ctx.HTML(200, tplForgotPassword)
  752. return
  753. }
  754. email := ctx.Query("email")
  755. ctx.Data["Email"] = email
  756. ctx.Data["IsResetRequest"] = true
  757. ctx.HTML(200, tplForgotPassword)
  758. }
  759. // ForgotPasswdPost response for forget password request
  760. func ForgotPasswdPost(ctx *context.Context) {
  761. ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title")
  762. if setting.MailService == nil {
  763. ctx.NotFound("ForgotPasswdPost", nil)
  764. return
  765. }
  766. ctx.Data["IsResetRequest"] = true
  767. email := ctx.Query("email")
  768. ctx.Data["Email"] = email
  769. u, err := models.GetUserByEmail(email)
  770. if err != nil {
  771. if models.IsErrUserNotExist(err) {
  772. ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
  773. ctx.Data["IsResetSent"] = true
  774. ctx.HTML(200, tplForgotPassword)
  775. return
  776. }
  777. ctx.ServerError("user.ResetPasswd(check existence)", err)
  778. return
  779. }
  780. if !u.IsLocal() && !u.IsOAuth2() {
  781. ctx.Data["Err_Email"] = true
  782. ctx.RenderWithErr(ctx.Tr("auth.non_local_account"), tplForgotPassword, nil)
  783. return
  784. }
  785. if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) {
  786. ctx.Data["ResendLimited"] = true
  787. ctx.HTML(200, tplForgotPassword)
  788. return
  789. }
  790. models.SendResetPasswordMail(ctx.Context, u)
  791. if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  792. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  793. }
  794. ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
  795. ctx.Data["IsResetSent"] = true
  796. ctx.HTML(200, tplForgotPassword)
  797. }
  798. // ResetPasswd render the reset password page
  799. func ResetPasswd(ctx *context.Context) {
  800. ctx.Data["Title"] = ctx.Tr("auth.reset_password")
  801. code := ctx.Query("code")
  802. if len(code) == 0 {
  803. ctx.Error(404)
  804. return
  805. }
  806. ctx.Data["Code"] = code
  807. ctx.Data["IsResetForm"] = true
  808. ctx.HTML(200, tplResetPassword)
  809. }
  810. // ResetPasswdPost response from reset password request
  811. func ResetPasswdPost(ctx *context.Context) {
  812. ctx.Data["Title"] = ctx.Tr("auth.reset_password")
  813. code := ctx.Query("code")
  814. if len(code) == 0 {
  815. ctx.Error(404)
  816. return
  817. }
  818. ctx.Data["Code"] = code
  819. if u := models.VerifyUserActiveCode(code); u != nil {
  820. // Validate password length.
  821. passwd := ctx.Query("password")
  822. if len(passwd) < setting.MinPasswordLength {
  823. ctx.Data["IsResetForm"] = true
  824. ctx.Data["Err_Password"] = true
  825. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplResetPassword, nil)
  826. return
  827. }
  828. var err error
  829. if u.Rands, err = models.GetUserSalt(); err != nil {
  830. ctx.ServerError("UpdateUser", err)
  831. return
  832. }
  833. if u.Salt, err = models.GetUserSalt(); err != nil {
  834. ctx.ServerError("UpdateUser", err)
  835. return
  836. }
  837. u.HashPassword(passwd)
  838. if err := models.UpdateUserCols(u, "passwd", "rands", "salt"); err != nil {
  839. ctx.ServerError("UpdateUser", err)
  840. return
  841. }
  842. log.Trace("User password reset: %s", u.Name)
  843. ctx.Redirect(setting.AppSubURL + "/user/login")
  844. return
  845. }
  846. ctx.Data["IsResetFailed"] = true
  847. ctx.HTML(200, tplResetPassword)
  848. }