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 34 kB

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