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

1264 lines
38 KiB

Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
7 years ago
  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. ctx.Data["user_name"] = gothUser.(goth.User).NickName
  570. ctx.Data["email"] = gothUser.(goth.User).Email
  571. ctx.HTML(200, tplLinkAccount)
  572. }
  573. // LinkAccountPostSignIn handle the coupling of external account with another account using signIn
  574. func LinkAccountPostSignIn(ctx *context.Context, signInForm auth.SignInForm) {
  575. ctx.Data["Title"] = ctx.Tr("link_account")
  576. ctx.Data["LinkAccountMode"] = true
  577. ctx.Data["LinkAccountModeSignIn"] = true
  578. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  579. ctx.Data["CaptchaType"] = setting.Service.CaptchaType
  580. ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
  581. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  582. ctx.Data["ShowRegistrationButton"] = false
  583. // use this to set the right link into the signIn and signUp templates in the link_account template
  584. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
  585. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
  586. gothUser := ctx.Session.Get("linkAccountGothUser")
  587. if gothUser == nil {
  588. ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
  589. return
  590. }
  591. if ctx.HasError() {
  592. ctx.HTML(200, tplLinkAccount)
  593. return
  594. }
  595. u, err := models.UserSignIn(signInForm.UserName, signInForm.Password)
  596. if err != nil {
  597. if models.IsErrUserNotExist(err) {
  598. ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplLinkAccount, &signInForm)
  599. } else {
  600. ctx.ServerError("UserLinkAccount", err)
  601. }
  602. return
  603. }
  604. // If this user is enrolled in 2FA, we can't sign the user in just yet.
  605. // Instead, redirect them to the 2FA authentication page.
  606. _, err = models.GetTwoFactorByUID(u.ID)
  607. if err != nil {
  608. if models.IsErrTwoFactorNotEnrolled(err) {
  609. err = models.LinkAccountToUser(u, gothUser.(goth.User))
  610. if err != nil {
  611. ctx.ServerError("UserLinkAccount", err)
  612. } else {
  613. handleSignIn(ctx, u, signInForm.Remember)
  614. }
  615. } else {
  616. ctx.ServerError("UserLinkAccount", err)
  617. }
  618. return
  619. }
  620. // User needs to use 2FA, save data and redirect to 2FA page.
  621. ctx.Session.Set("twofaUid", u.ID)
  622. ctx.Session.Set("twofaRemember", signInForm.Remember)
  623. ctx.Session.Set("linkAccount", true)
  624. // If U2F is enrolled -> Redirect to U2F instead
  625. regs, err := models.GetU2FRegistrationsByUID(u.ID)
  626. if err == nil && len(regs) > 0 {
  627. ctx.Redirect(setting.AppSubURL + "/user/u2f")
  628. return
  629. }
  630. ctx.Redirect(setting.AppSubURL + "/user/two_factor")
  631. }
  632. // LinkAccountPostRegister handle the creation of a new account for an external account using signUp
  633. func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
  634. ctx.Data["Title"] = ctx.Tr("link_account")
  635. ctx.Data["LinkAccountMode"] = true
  636. ctx.Data["LinkAccountModeRegister"] = true
  637. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  638. ctx.Data["CaptchaType"] = setting.Service.CaptchaType
  639. ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
  640. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  641. ctx.Data["ShowRegistrationButton"] = false
  642. // use this to set the right link into the signIn and signUp templates in the link_account template
  643. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
  644. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
  645. gothUser := ctx.Session.Get("linkAccountGothUser")
  646. if gothUser == nil {
  647. ctx.ServerError("UserSignUp", errors.New("not in LinkAccount session"))
  648. return
  649. }
  650. if ctx.HasError() {
  651. ctx.HTML(200, tplLinkAccount)
  652. return
  653. }
  654. if setting.Service.DisableRegistration {
  655. ctx.Error(403)
  656. return
  657. }
  658. if setting.Service.EnableCaptcha && setting.Service.CaptchaType == setting.ImageCaptcha && !cpt.VerifyReq(ctx.Req) {
  659. ctx.Data["Err_Captcha"] = true
  660. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplLinkAccount, &form)
  661. return
  662. }
  663. if setting.Service.EnableCaptcha && setting.Service.CaptchaType == setting.ReCaptcha {
  664. valid, _ := recaptcha.Verify(form.GRecaptchaResponse)
  665. if !valid {
  666. ctx.Data["Err_Captcha"] = true
  667. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplLinkAccount, &form)
  668. return
  669. }
  670. }
  671. if (len(strings.TrimSpace(form.Password)) > 0 || len(strings.TrimSpace(form.Retype)) > 0) && form.Password != form.Retype {
  672. ctx.Data["Err_Password"] = true
  673. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplLinkAccount, &form)
  674. return
  675. }
  676. if len(strings.TrimSpace(form.Password)) > 0 && len(form.Password) < setting.MinPasswordLength {
  677. ctx.Data["Err_Password"] = true
  678. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplLinkAccount, &form)
  679. return
  680. }
  681. loginSource, err := models.GetActiveOAuth2LoginSourceByName(gothUser.(goth.User).Provider)
  682. if err != nil {
  683. ctx.ServerError("CreateUser", err)
  684. }
  685. u := &models.User{
  686. Name: form.UserName,
  687. Email: form.Email,
  688. Passwd: form.Password,
  689. IsActive: !setting.Service.RegisterEmailConfirm,
  690. LoginType: models.LoginOAuth2,
  691. LoginSource: loginSource.ID,
  692. LoginName: gothUser.(goth.User).UserID,
  693. }
  694. if err := models.CreateUser(u); err != nil {
  695. switch {
  696. case models.IsErrUserAlreadyExist(err):
  697. ctx.Data["Err_UserName"] = true
  698. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplLinkAccount, &form)
  699. case models.IsErrEmailAlreadyUsed(err):
  700. ctx.Data["Err_Email"] = true
  701. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplLinkAccount, &form)
  702. case models.IsErrNameReserved(err):
  703. ctx.Data["Err_UserName"] = true
  704. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplLinkAccount, &form)
  705. case models.IsErrNamePatternNotAllowed(err):
  706. ctx.Data["Err_UserName"] = true
  707. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplLinkAccount, &form)
  708. default:
  709. ctx.ServerError("CreateUser", err)
  710. }
  711. return
  712. }
  713. log.Trace("Account created: %s", u.Name)
  714. // Auto-set admin for the only user.
  715. if models.CountUsers() == 1 {
  716. u.IsAdmin = true
  717. u.IsActive = true
  718. u.SetLastLogin()
  719. if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
  720. ctx.ServerError("UpdateUser", err)
  721. return
  722. }
  723. }
  724. // Send confirmation email
  725. if setting.Service.RegisterEmailConfirm && u.ID > 1 {
  726. models.SendActivateAccountMail(ctx.Context, u)
  727. ctx.Data["IsSendRegisterMail"] = true
  728. ctx.Data["Email"] = u.Email
  729. ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  730. ctx.HTML(200, TplActivate)
  731. if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  732. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  733. }
  734. return
  735. }
  736. ctx.Redirect(setting.AppSubURL + "/user/login")
  737. }
  738. // SignOut sign out from login status
  739. func SignOut(ctx *context.Context) {
  740. ctx.Session.Delete("uid")
  741. ctx.Session.Delete("uname")
  742. ctx.Session.Delete("socialId")
  743. ctx.Session.Delete("socialName")
  744. ctx.Session.Delete("socialEmail")
  745. ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  746. ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  747. ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
  748. 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.
  749. ctx.Redirect(setting.AppSubURL + "/")
  750. }
  751. // SignUp render the register page
  752. func SignUp(ctx *context.Context) {
  753. ctx.Data["Title"] = ctx.Tr("sign_up")
  754. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
  755. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  756. ctx.Data["CaptchaType"] = setting.Service.CaptchaType
  757. ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
  758. ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
  759. ctx.HTML(200, tplSignUp)
  760. }
  761. // SignUpPost response for sign up information submission
  762. func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
  763. ctx.Data["Title"] = ctx.Tr("sign_up")
  764. ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
  765. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  766. ctx.Data["CaptchaType"] = setting.Service.CaptchaType
  767. ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
  768. //Permission denied if DisableRegistration or AllowOnlyExternalRegistration options are true
  769. if !setting.Service.ShowRegistrationButton {
  770. ctx.Error(403)
  771. return
  772. }
  773. if ctx.HasError() {
  774. ctx.HTML(200, tplSignUp)
  775. return
  776. }
  777. if setting.Service.EnableCaptcha && setting.Service.CaptchaType == setting.ImageCaptcha && !cpt.VerifyReq(ctx.Req) {
  778. ctx.Data["Err_Captcha"] = true
  779. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplSignUp, &form)
  780. return
  781. }
  782. if setting.Service.EnableCaptcha && setting.Service.CaptchaType == setting.ReCaptcha {
  783. valid, _ := recaptcha.Verify(form.GRecaptchaResponse)
  784. if !valid {
  785. ctx.Data["Err_Captcha"] = true
  786. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplSignUp, &form)
  787. return
  788. }
  789. }
  790. if form.Password != form.Retype {
  791. ctx.Data["Err_Password"] = true
  792. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplSignUp, &form)
  793. return
  794. }
  795. if len(form.Password) < setting.MinPasswordLength {
  796. ctx.Data["Err_Password"] = true
  797. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplSignUp, &form)
  798. return
  799. }
  800. u := &models.User{
  801. Name: form.UserName,
  802. Email: form.Email,
  803. Passwd: form.Password,
  804. IsActive: !setting.Service.RegisterEmailConfirm,
  805. }
  806. if err := models.CreateUser(u); err != nil {
  807. switch {
  808. case models.IsErrUserAlreadyExist(err):
  809. ctx.Data["Err_UserName"] = true
  810. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplSignUp, &form)
  811. case models.IsErrEmailAlreadyUsed(err):
  812. ctx.Data["Err_Email"] = true
  813. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignUp, &form)
  814. case models.IsErrNameReserved(err):
  815. ctx.Data["Err_UserName"] = true
  816. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplSignUp, &form)
  817. case models.IsErrNamePatternNotAllowed(err):
  818. ctx.Data["Err_UserName"] = true
  819. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplSignUp, &form)
  820. default:
  821. ctx.ServerError("CreateUser", err)
  822. }
  823. return
  824. }
  825. log.Trace("Account created: %s", u.Name)
  826. // Auto-set admin for the only user.
  827. if models.CountUsers() == 1 {
  828. u.IsAdmin = true
  829. u.IsActive = true
  830. u.SetLastLogin()
  831. if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
  832. ctx.ServerError("UpdateUser", err)
  833. return
  834. }
  835. }
  836. // Send confirmation email, no need for social account.
  837. if setting.Service.RegisterEmailConfirm && u.ID > 1 {
  838. models.SendActivateAccountMail(ctx.Context, u)
  839. ctx.Data["IsSendRegisterMail"] = true
  840. ctx.Data["Email"] = u.Email
  841. ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  842. ctx.HTML(200, TplActivate)
  843. if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  844. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  845. }
  846. return
  847. }
  848. ctx.Flash.Success(ctx.Tr("auth.sign_up_successful"))
  849. handleSignInFull(ctx, u, false, true)
  850. }
  851. // Activate render activate user page
  852. func Activate(ctx *context.Context) {
  853. code := ctx.Query("code")
  854. if len(code) == 0 {
  855. ctx.Data["IsActivatePage"] = true
  856. if ctx.User.IsActive {
  857. ctx.Error(404)
  858. return
  859. }
  860. // Resend confirmation email.
  861. if setting.Service.RegisterEmailConfirm {
  862. if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
  863. ctx.Data["ResendLimited"] = true
  864. } else {
  865. ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
  866. models.SendActivateAccountMail(ctx.Context, ctx.User)
  867. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  868. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  869. }
  870. }
  871. } else {
  872. ctx.Data["ServiceNotEnabled"] = true
  873. }
  874. ctx.HTML(200, TplActivate)
  875. return
  876. }
  877. // Verify code.
  878. if user := models.VerifyUserActiveCode(code); user != nil {
  879. user.IsActive = true
  880. var err error
  881. if user.Rands, err = models.GetUserSalt(); err != nil {
  882. ctx.ServerError("UpdateUser", err)
  883. return
  884. }
  885. if err := models.UpdateUserCols(user, "is_active", "rands"); err != nil {
  886. if models.IsErrUserNotExist(err) {
  887. ctx.Error(404)
  888. } else {
  889. ctx.ServerError("UpdateUser", err)
  890. }
  891. return
  892. }
  893. log.Trace("User activated: %s", user.Name)
  894. ctx.Session.Set("uid", user.ID)
  895. ctx.Session.Set("uname", user.Name)
  896. ctx.Flash.Success(ctx.Tr("auth.account_activated"))
  897. ctx.Redirect(setting.AppSubURL + "/")
  898. return
  899. }
  900. ctx.Data["IsActivateFailed"] = true
  901. ctx.HTML(200, TplActivate)
  902. }
  903. // ActivateEmail render the activate email page
  904. func ActivateEmail(ctx *context.Context) {
  905. code := ctx.Query("code")
  906. emailStr := ctx.Query("email")
  907. // Verify code.
  908. if email := models.VerifyActiveEmailCode(code, emailStr); email != nil {
  909. if err := email.Activate(); err != nil {
  910. ctx.ServerError("ActivateEmail", err)
  911. }
  912. log.Trace("Email activated: %s", email.Email)
  913. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  914. }
  915. ctx.Redirect(setting.AppSubURL + "/user/settings/email")
  916. return
  917. }
  918. // ForgotPasswd render the forget pasword page
  919. func ForgotPasswd(ctx *context.Context) {
  920. ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title")
  921. if setting.MailService == nil {
  922. ctx.Data["IsResetDisable"] = true
  923. ctx.HTML(200, tplForgotPassword)
  924. return
  925. }
  926. email := ctx.Query("email")
  927. ctx.Data["Email"] = email
  928. ctx.Data["IsResetRequest"] = true
  929. ctx.HTML(200, tplForgotPassword)
  930. }
  931. // ForgotPasswdPost response for forget password request
  932. func ForgotPasswdPost(ctx *context.Context) {
  933. ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title")
  934. if setting.MailService == nil {
  935. ctx.NotFound("ForgotPasswdPost", nil)
  936. return
  937. }
  938. ctx.Data["IsResetRequest"] = true
  939. email := ctx.Query("email")
  940. ctx.Data["Email"] = email
  941. u, err := models.GetUserByEmail(email)
  942. if err != nil {
  943. if models.IsErrUserNotExist(err) {
  944. ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
  945. ctx.Data["IsResetSent"] = true
  946. ctx.HTML(200, tplForgotPassword)
  947. return
  948. }
  949. ctx.ServerError("user.ResetPasswd(check existence)", err)
  950. return
  951. }
  952. if !u.IsLocal() && !u.IsOAuth2() {
  953. ctx.Data["Err_Email"] = true
  954. ctx.RenderWithErr(ctx.Tr("auth.non_local_account"), tplForgotPassword, nil)
  955. return
  956. }
  957. if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) {
  958. ctx.Data["ResendLimited"] = true
  959. ctx.HTML(200, tplForgotPassword)
  960. return
  961. }
  962. models.SendResetPasswordMail(ctx.Context, u)
  963. if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  964. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  965. }
  966. ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
  967. ctx.Data["IsResetSent"] = true
  968. ctx.HTML(200, tplForgotPassword)
  969. }
  970. // ResetPasswd render the reset password page
  971. func ResetPasswd(ctx *context.Context) {
  972. ctx.Data["Title"] = ctx.Tr("auth.reset_password")
  973. code := ctx.Query("code")
  974. if len(code) == 0 {
  975. ctx.Error(404)
  976. return
  977. }
  978. ctx.Data["Code"] = code
  979. ctx.Data["IsResetForm"] = true
  980. ctx.HTML(200, tplResetPassword)
  981. }
  982. // ResetPasswdPost response from reset password request
  983. func ResetPasswdPost(ctx *context.Context) {
  984. ctx.Data["Title"] = ctx.Tr("auth.reset_password")
  985. code := ctx.Query("code")
  986. if len(code) == 0 {
  987. ctx.Error(404)
  988. return
  989. }
  990. ctx.Data["Code"] = code
  991. if u := models.VerifyUserActiveCode(code); u != nil {
  992. // Validate password length.
  993. passwd := ctx.Query("password")
  994. if len(passwd) < setting.MinPasswordLength {
  995. ctx.Data["IsResetForm"] = true
  996. ctx.Data["Err_Password"] = true
  997. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplResetPassword, nil)
  998. return
  999. }
  1000. var err error
  1001. if u.Rands, err = models.GetUserSalt(); err != nil {
  1002. ctx.ServerError("UpdateUser", err)
  1003. return
  1004. }
  1005. if u.Salt, err = models.GetUserSalt(); err != nil {
  1006. ctx.ServerError("UpdateUser", err)
  1007. return
  1008. }
  1009. u.HashPassword(passwd)
  1010. u.MustChangePassword = false
  1011. if err := models.UpdateUserCols(u, "must_change_password", "passwd", "rands", "salt"); err != nil {
  1012. ctx.ServerError("UpdateUser", err)
  1013. return
  1014. }
  1015. log.Trace("User password reset: %s", u.Name)
  1016. ctx.Redirect(setting.AppSubURL + "/user/login")
  1017. return
  1018. }
  1019. ctx.Data["IsResetFailed"] = true
  1020. ctx.HTML(200, tplResetPassword)
  1021. }
  1022. // MustChangePassword renders the page to change a user's password
  1023. func MustChangePassword(ctx *context.Context) {
  1024. ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
  1025. ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/settings/change_password"
  1026. ctx.HTML(200, tplMustChangePassword)
  1027. }
  1028. // MustChangePasswordPost response for updating a user's password after his/her
  1029. // account was created by an admin
  1030. func MustChangePasswordPost(ctx *context.Context, cpt *captcha.Captcha, form auth.MustChangePasswordForm) {
  1031. ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
  1032. ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/settings/change_password"
  1033. if ctx.HasError() {
  1034. ctx.HTML(200, tplMustChangePassword)
  1035. return
  1036. }
  1037. u := ctx.User
  1038. // Make sure only requests for users who are eligible to change their password via
  1039. // this method passes through
  1040. if !u.MustChangePassword {
  1041. ctx.ServerError("MustUpdatePassword", errors.New("cannot update password.. Please visit the settings page"))
  1042. return
  1043. }
  1044. if form.Password != form.Retype {
  1045. ctx.Data["Err_Password"] = true
  1046. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplMustChangePassword, &form)
  1047. return
  1048. }
  1049. if len(form.Password) < setting.MinPasswordLength {
  1050. ctx.Data["Err_Password"] = true
  1051. ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplMustChangePassword, &form)
  1052. return
  1053. }
  1054. var err error
  1055. if u.Salt, err = models.GetUserSalt(); err != nil {
  1056. ctx.ServerError("UpdateUser", err)
  1057. return
  1058. }
  1059. u.HashPassword(form.Password)
  1060. u.MustChangePassword = false
  1061. if err := models.UpdateUserCols(u, "must_change_password", "passwd", "salt"); err != nil {
  1062. ctx.ServerError("UpdateUser", err)
  1063. return
  1064. }
  1065. ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
  1066. log.Trace("User updated password: %s", u.Name)
  1067. if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 && !util.IsExternalURL(redirectTo) {
  1068. ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
  1069. ctx.RedirectToFirst(redirectTo)
  1070. return
  1071. }
  1072. ctx.Redirect(setting.AppSubURL + "/")
  1073. }