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

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