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

auths.go 10 kB

12 years ago
12 years ago
12 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
12 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
12 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
12 years ago
12 years ago
12 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
12 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package admin
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/auth"
  9. "code.gitea.io/gitea/modules/auth/ldap"
  10. "code.gitea.io/gitea/modules/auth/oauth2"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. "github.com/Unknwon/com"
  16. "github.com/go-xorm/core"
  17. )
  18. const (
  19. tplAuths base.TplName = "admin/auth/list"
  20. tplAuthNew base.TplName = "admin/auth/new"
  21. tplAuthEdit base.TplName = "admin/auth/edit"
  22. )
  23. // Authentications show authentication config page
  24. func Authentications(ctx *context.Context) {
  25. ctx.Data["Title"] = ctx.Tr("admin.authentication")
  26. ctx.Data["PageIsAdmin"] = true
  27. ctx.Data["PageIsAdminAuthentications"] = true
  28. var err error
  29. ctx.Data["Sources"], err = models.LoginSources()
  30. if err != nil {
  31. ctx.ServerError("LoginSources", err)
  32. return
  33. }
  34. ctx.Data["Total"] = models.CountLoginSources()
  35. ctx.HTML(200, tplAuths)
  36. }
  37. type dropdownItem struct {
  38. Name string
  39. Type interface{}
  40. }
  41. var (
  42. authSources = []dropdownItem{
  43. {models.LoginNames[models.LoginLDAP], models.LoginLDAP},
  44. {models.LoginNames[models.LoginDLDAP], models.LoginDLDAP},
  45. {models.LoginNames[models.LoginSMTP], models.LoginSMTP},
  46. {models.LoginNames[models.LoginPAM], models.LoginPAM},
  47. {models.LoginNames[models.LoginOAuth2], models.LoginOAuth2},
  48. }
  49. securityProtocols = []dropdownItem{
  50. {models.SecurityProtocolNames[ldap.SecurityProtocolUnencrypted], ldap.SecurityProtocolUnencrypted},
  51. {models.SecurityProtocolNames[ldap.SecurityProtocolLDAPS], ldap.SecurityProtocolLDAPS},
  52. {models.SecurityProtocolNames[ldap.SecurityProtocolStartTLS], ldap.SecurityProtocolStartTLS},
  53. }
  54. )
  55. // NewAuthSource render adding a new auth source page
  56. func NewAuthSource(ctx *context.Context) {
  57. ctx.Data["Title"] = ctx.Tr("admin.auths.new")
  58. ctx.Data["PageIsAdmin"] = true
  59. ctx.Data["PageIsAdminAuthentications"] = true
  60. ctx.Data["type"] = models.LoginLDAP
  61. ctx.Data["CurrentTypeName"] = models.LoginNames[models.LoginLDAP]
  62. ctx.Data["CurrentSecurityProtocol"] = models.SecurityProtocolNames[ldap.SecurityProtocolUnencrypted]
  63. ctx.Data["smtp_auth"] = "PLAIN"
  64. ctx.Data["is_active"] = true
  65. ctx.Data["is_sync_enabled"] = true
  66. ctx.Data["AuthSources"] = authSources
  67. ctx.Data["SecurityProtocols"] = securityProtocols
  68. ctx.Data["SMTPAuths"] = models.SMTPAuths
  69. ctx.Data["OAuth2Providers"] = models.OAuth2Providers
  70. ctx.Data["OAuth2DefaultCustomURLMappings"] = models.OAuth2DefaultCustomURLMappings
  71. // only the first as default
  72. for key := range models.OAuth2Providers {
  73. ctx.Data["oauth2_provider"] = key
  74. break
  75. }
  76. ctx.HTML(200, tplAuthNew)
  77. }
  78. func parseLDAPConfig(form auth.AuthenticationForm) *models.LDAPConfig {
  79. var pageSize uint32
  80. if form.UsePagedSearch {
  81. pageSize = uint32(form.SearchPageSize)
  82. }
  83. return &models.LDAPConfig{
  84. Source: &ldap.Source{
  85. Name: form.Name,
  86. Host: form.Host,
  87. Port: form.Port,
  88. SecurityProtocol: ldap.SecurityProtocol(form.SecurityProtocol),
  89. SkipVerify: form.SkipVerify,
  90. BindDN: form.BindDN,
  91. UserDN: form.UserDN,
  92. BindPassword: form.BindPassword,
  93. UserBase: form.UserBase,
  94. AttributeUsername: form.AttributeUsername,
  95. AttributeName: form.AttributeName,
  96. AttributeSurname: form.AttributeSurname,
  97. AttributeMail: form.AttributeMail,
  98. AttributesInBind: form.AttributesInBind,
  99. SearchPageSize: pageSize,
  100. Filter: form.Filter,
  101. AdminFilter: form.AdminFilter,
  102. Enabled: true,
  103. },
  104. }
  105. }
  106. func parseSMTPConfig(form auth.AuthenticationForm) *models.SMTPConfig {
  107. return &models.SMTPConfig{
  108. Auth: form.SMTPAuth,
  109. Host: form.SMTPHost,
  110. Port: form.SMTPPort,
  111. AllowedDomains: form.AllowedDomains,
  112. TLS: form.TLS,
  113. SkipVerify: form.SkipVerify,
  114. }
  115. }
  116. func parseOAuth2Config(form auth.AuthenticationForm) *models.OAuth2Config {
  117. var customURLMapping *oauth2.CustomURLMapping
  118. if form.Oauth2UseCustomURL {
  119. customURLMapping = &oauth2.CustomURLMapping{
  120. TokenURL: form.Oauth2TokenURL,
  121. AuthURL: form.Oauth2AuthURL,
  122. ProfileURL: form.Oauth2ProfileURL,
  123. EmailURL: form.Oauth2EmailURL,
  124. }
  125. } else {
  126. customURLMapping = nil
  127. }
  128. return &models.OAuth2Config{
  129. Provider: form.Oauth2Provider,
  130. ClientID: form.Oauth2Key,
  131. ClientSecret: form.Oauth2Secret,
  132. OpenIDConnectAutoDiscoveryURL: form.OpenIDConnectAutoDiscoveryURL,
  133. CustomURLMapping: customURLMapping,
  134. }
  135. }
  136. // NewAuthSourcePost response for adding an auth source
  137. func NewAuthSourcePost(ctx *context.Context, form auth.AuthenticationForm) {
  138. ctx.Data["Title"] = ctx.Tr("admin.auths.new")
  139. ctx.Data["PageIsAdmin"] = true
  140. ctx.Data["PageIsAdminAuthentications"] = true
  141. ctx.Data["CurrentTypeName"] = models.LoginNames[models.LoginType(form.Type)]
  142. ctx.Data["CurrentSecurityProtocol"] = models.SecurityProtocolNames[ldap.SecurityProtocol(form.SecurityProtocol)]
  143. ctx.Data["AuthSources"] = authSources
  144. ctx.Data["SecurityProtocols"] = securityProtocols
  145. ctx.Data["SMTPAuths"] = models.SMTPAuths
  146. ctx.Data["OAuth2Providers"] = models.OAuth2Providers
  147. ctx.Data["OAuth2DefaultCustomURLMappings"] = models.OAuth2DefaultCustomURLMappings
  148. hasTLS := false
  149. var config core.Conversion
  150. switch models.LoginType(form.Type) {
  151. case models.LoginLDAP, models.LoginDLDAP:
  152. config = parseLDAPConfig(form)
  153. hasTLS = ldap.SecurityProtocol(form.SecurityProtocol) > ldap.SecurityProtocolUnencrypted
  154. case models.LoginSMTP:
  155. config = parseSMTPConfig(form)
  156. hasTLS = true
  157. case models.LoginPAM:
  158. config = &models.PAMConfig{
  159. ServiceName: form.PAMServiceName,
  160. }
  161. case models.LoginOAuth2:
  162. config = parseOAuth2Config(form)
  163. default:
  164. ctx.Error(400)
  165. return
  166. }
  167. ctx.Data["HasTLS"] = hasTLS
  168. if ctx.HasError() {
  169. ctx.HTML(200, tplAuthNew)
  170. return
  171. }
  172. if err := models.CreateLoginSource(&models.LoginSource{
  173. Type: models.LoginType(form.Type),
  174. Name: form.Name,
  175. IsActived: form.IsActive,
  176. IsSyncEnabled: form.IsSyncEnabled,
  177. Cfg: config,
  178. }); err != nil {
  179. if models.IsErrLoginSourceAlreadyExist(err) {
  180. ctx.Data["Err_Name"] = true
  181. ctx.RenderWithErr(ctx.Tr("admin.auths.login_source_exist", err.(models.ErrLoginSourceAlreadyExist).Name), tplAuthNew, form)
  182. } else {
  183. ctx.ServerError("CreateSource", err)
  184. }
  185. return
  186. }
  187. log.Trace("Authentication created by admin(%s): %s", ctx.User.Name, form.Name)
  188. ctx.Flash.Success(ctx.Tr("admin.auths.new_success", form.Name))
  189. ctx.Redirect(setting.AppSubURL + "/admin/auths")
  190. }
  191. // EditAuthSource render editing auth source page
  192. func EditAuthSource(ctx *context.Context) {
  193. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  194. ctx.Data["PageIsAdmin"] = true
  195. ctx.Data["PageIsAdminAuthentications"] = true
  196. ctx.Data["SecurityProtocols"] = securityProtocols
  197. ctx.Data["SMTPAuths"] = models.SMTPAuths
  198. ctx.Data["OAuth2Providers"] = models.OAuth2Providers
  199. ctx.Data["OAuth2DefaultCustomURLMappings"] = models.OAuth2DefaultCustomURLMappings
  200. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  201. if err != nil {
  202. ctx.ServerError("GetLoginSourceByID", err)
  203. return
  204. }
  205. ctx.Data["Source"] = source
  206. ctx.Data["HasTLS"] = source.HasTLS()
  207. if source.IsOAuth2() {
  208. ctx.Data["CurrentOAuth2Provider"] = models.OAuth2Providers[source.OAuth2().Provider]
  209. }
  210. ctx.HTML(200, tplAuthEdit)
  211. }
  212. // EditAuthSourcePost response for editing auth source
  213. func EditAuthSourcePost(ctx *context.Context, form auth.AuthenticationForm) {
  214. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  215. ctx.Data["PageIsAdmin"] = true
  216. ctx.Data["PageIsAdminAuthentications"] = true
  217. ctx.Data["SMTPAuths"] = models.SMTPAuths
  218. ctx.Data["OAuth2Providers"] = models.OAuth2Providers
  219. ctx.Data["OAuth2DefaultCustomURLMappings"] = models.OAuth2DefaultCustomURLMappings
  220. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  221. if err != nil {
  222. ctx.ServerError("GetLoginSourceByID", err)
  223. return
  224. }
  225. ctx.Data["Source"] = source
  226. ctx.Data["HasTLS"] = source.HasTLS()
  227. if ctx.HasError() {
  228. ctx.HTML(200, tplAuthEdit)
  229. return
  230. }
  231. var config core.Conversion
  232. switch models.LoginType(form.Type) {
  233. case models.LoginLDAP, models.LoginDLDAP:
  234. config = parseLDAPConfig(form)
  235. case models.LoginSMTP:
  236. config = parseSMTPConfig(form)
  237. case models.LoginPAM:
  238. config = &models.PAMConfig{
  239. ServiceName: form.PAMServiceName,
  240. }
  241. case models.LoginOAuth2:
  242. config = parseOAuth2Config(form)
  243. default:
  244. ctx.Error(400)
  245. return
  246. }
  247. source.Name = form.Name
  248. source.IsActived = form.IsActive
  249. source.IsSyncEnabled = form.IsSyncEnabled
  250. source.Cfg = config
  251. if err := models.UpdateSource(source); err != nil {
  252. if models.IsErrOpenIDConnectInitialize(err) {
  253. ctx.Flash.Error(err.Error(), true)
  254. ctx.HTML(200, tplAuthEdit)
  255. } else {
  256. ctx.ServerError("UpdateSource", err)
  257. }
  258. return
  259. }
  260. log.Trace("Authentication changed by admin(%s): %d", ctx.User.Name, source.ID)
  261. ctx.Flash.Success(ctx.Tr("admin.auths.update_success"))
  262. ctx.Redirect(setting.AppSubURL + "/admin/auths/" + com.ToStr(form.ID))
  263. }
  264. // DeleteAuthSource response for deleting an auth source
  265. func DeleteAuthSource(ctx *context.Context) {
  266. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  267. if err != nil {
  268. ctx.ServerError("GetLoginSourceByID", err)
  269. return
  270. }
  271. if err = models.DeleteSource(source); err != nil {
  272. if models.IsErrLoginSourceInUse(err) {
  273. ctx.Flash.Error(ctx.Tr("admin.auths.still_in_used"))
  274. } else {
  275. ctx.Flash.Error(fmt.Sprintf("DeleteSource: %v", err))
  276. }
  277. ctx.JSON(200, map[string]interface{}{
  278. "redirect": setting.AppSubURL + "/admin/auths/" + ctx.Params(":authid"),
  279. })
  280. return
  281. }
  282. log.Trace("Authentication deleted by admin(%s): %d", ctx.User.Name, source.ID)
  283. ctx.Flash.Success(ctx.Tr("admin.auths.deletion_success"))
  284. ctx.JSON(200, map[string]interface{}{
  285. "redirect": setting.AppSubURL + "/admin/auths",
  286. })
  287. }