auths.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package admin
  7. import (
  8. "fmt"
  9. "gitote/gitote/models"
  10. "gitote/gitote/pkg/auth/ldap"
  11. "gitote/gitote/pkg/context"
  12. "gitote/gitote/pkg/form"
  13. "gitote/gitote/pkg/setting"
  14. "github.com/Unknwon/com"
  15. "github.com/go-xorm/core"
  16. log "gopkg.in/clog.v1"
  17. )
  18. const (
  19. // AUTHS list page template
  20. AUTHS = "admin/auth/list"
  21. // AUTH_NEW page template
  22. AUTH_NEW = "admin/auth/new"
  23. // AUTH_EDIT page template
  24. AUTH_EDIT = "admin/auth/edit"
  25. )
  26. // Authentications shows authentication page
  27. func Authentications(c *context.Context) {
  28. c.Title("admin.authentication")
  29. c.PageIs("Admin")
  30. c.PageIs("AdminAuthentications")
  31. var err error
  32. c.Data["Sources"], err = models.LoginSources()
  33. if err != nil {
  34. c.ServerError("LoginSources", err)
  35. return
  36. }
  37. c.Data["Total"] = models.CountLoginSources()
  38. c.Success(AUTHS)
  39. }
  40. type dropdownItem struct {
  41. Name string
  42. Type interface{}
  43. }
  44. var (
  45. authSources = []dropdownItem{
  46. {models.LoginNames[models.LOGIN_LDAP], models.LOGIN_LDAP},
  47. {models.LoginNames[models.LOGIN_DLDAP], models.LOGIN_DLDAP},
  48. {models.LoginNames[models.LOGIN_SMTP], models.LOGIN_SMTP},
  49. {models.LoginNames[models.LOGIN_PAM], models.LOGIN_PAM},
  50. }
  51. securityProtocols = []dropdownItem{
  52. {models.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_UNENCRYPTED], ldap.SECURITY_PROTOCOL_UNENCRYPTED},
  53. {models.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_LDAPS], ldap.SECURITY_PROTOCOL_LDAPS},
  54. {models.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_START_TLS], ldap.SECURITY_PROTOCOL_START_TLS},
  55. }
  56. )
  57. // NewAuthSource shows create auth page
  58. func NewAuthSource(c *context.Context) {
  59. c.Title("admin.auths.new")
  60. c.PageIs("Admin")
  61. c.PageIs("AdminAuthentications")
  62. c.Data["type"] = models.LOGIN_LDAP
  63. c.Data["CurrentTypeName"] = models.LoginNames[models.LOGIN_LDAP]
  64. c.Data["CurrentSecurityProtocol"] = models.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_UNENCRYPTED]
  65. c.Data["smtp_auth"] = "PLAIN"
  66. c.Data["is_active"] = true
  67. c.Data["is_default"] = true
  68. c.Data["AuthSources"] = authSources
  69. c.Data["SecurityProtocols"] = securityProtocols
  70. c.Data["SMTPAuths"] = models.SMTPAuths
  71. c.Success(AUTH_NEW)
  72. }
  73. func parseLDAPConfig(f form.Authentication) *models.LDAPConfig {
  74. return &models.LDAPConfig{
  75. Source: &ldap.Source{
  76. Host: f.Host,
  77. Port: f.Port,
  78. SecurityProtocol: ldap.SecurityProtocol(f.SecurityProtocol),
  79. SkipVerify: f.SkipVerify,
  80. BindDN: f.BindDN,
  81. UserDN: f.UserDN,
  82. BindPassword: f.BindPassword,
  83. UserBase: f.UserBase,
  84. AttributeUsername: f.AttributeUsername,
  85. AttributeName: f.AttributeName,
  86. AttributeSurname: f.AttributeSurname,
  87. AttributeMail: f.AttributeMail,
  88. AttributesInBind: f.AttributesInBind,
  89. Filter: f.Filter,
  90. GroupEnabled: f.GroupEnabled,
  91. GroupDN: f.GroupDN,
  92. GroupFilter: f.GroupFilter,
  93. GroupMemberUID: f.GroupMemberUID,
  94. UserUID: f.UserUID,
  95. AdminFilter: f.AdminFilter,
  96. },
  97. }
  98. }
  99. func parseSMTPConfig(f form.Authentication) *models.SMTPConfig {
  100. return &models.SMTPConfig{
  101. Auth: f.SMTPAuth,
  102. Host: f.SMTPHost,
  103. Port: f.SMTPPort,
  104. AllowedDomains: f.AllowedDomains,
  105. TLS: f.TLS,
  106. SkipVerify: f.SkipVerify,
  107. }
  108. }
  109. // NewAuthSourcePost creates a new auth source
  110. func NewAuthSourcePost(c *context.Context, f form.Authentication) {
  111. c.Title("admin.auths.new")
  112. c.PageIs("Admin")
  113. c.PageIs("AdminAuthentications")
  114. c.Data["CurrentTypeName"] = models.LoginNames[models.LoginType(f.Type)]
  115. c.Data["CurrentSecurityProtocol"] = models.SecurityProtocolNames[ldap.SecurityProtocol(f.SecurityProtocol)]
  116. c.Data["AuthSources"] = authSources
  117. c.Data["SecurityProtocols"] = securityProtocols
  118. c.Data["SMTPAuths"] = models.SMTPAuths
  119. hasTLS := false
  120. var config core.Conversion
  121. switch models.LoginType(f.Type) {
  122. case models.LOGIN_LDAP, models.LOGIN_DLDAP:
  123. config = parseLDAPConfig(f)
  124. hasTLS = ldap.SecurityProtocol(f.SecurityProtocol) > ldap.SECURITY_PROTOCOL_UNENCRYPTED
  125. case models.LOGIN_SMTP:
  126. config = parseSMTPConfig(f)
  127. hasTLS = true
  128. case models.LOGIN_PAM:
  129. config = &models.PAMConfig{
  130. ServiceName: f.PAMServiceName,
  131. }
  132. default:
  133. c.Error(400)
  134. return
  135. }
  136. c.Data["HasTLS"] = hasTLS
  137. if c.HasError() {
  138. c.Success(AUTH_NEW)
  139. return
  140. }
  141. if err := models.CreateLoginSource(&models.LoginSource{
  142. Type: models.LoginType(f.Type),
  143. Name: f.Name,
  144. IsActived: f.IsActive,
  145. IsDefault: f.IsDefault,
  146. Cfg: config,
  147. }); err != nil {
  148. if models.IsErrLoginSourceAlreadyExist(err) {
  149. c.Data["Err_Name"] = true
  150. c.RenderWithErr(c.Tr("admin.auths.login_source_exist", err.(models.ErrLoginSourceAlreadyExist).Name), AUTH_NEW, f)
  151. } else {
  152. c.ServerError("CreateSource", err)
  153. }
  154. return
  155. }
  156. log.Trace("Authentication created by admin(%s): %s", c.User.Name, f.Name)
  157. c.Flash.Success(c.Tr("admin.auths.new_success", f.Name))
  158. c.Redirect(setting.AppSubURL + "/admin/auths")
  159. }
  160. // EditAuthSource shows edit auth source page
  161. func EditAuthSource(c *context.Context) {
  162. c.Title("admin.auths.edit")
  163. c.PageIs("Admin")
  164. c.PageIs("AdminAuthentications")
  165. c.Data["SecurityProtocols"] = securityProtocols
  166. c.Data["SMTPAuths"] = models.SMTPAuths
  167. source, err := models.GetLoginSourceByID(c.ParamsInt64(":authid"))
  168. if err != nil {
  169. c.ServerError("GetLoginSourceByID", err)
  170. return
  171. }
  172. c.Data["Source"] = source
  173. c.Data["HasTLS"] = source.HasTLS()
  174. c.Success(AUTH_EDIT)
  175. }
  176. // EditAuthSourcePost edits an auth source
  177. func EditAuthSourcePost(c *context.Context, f form.Authentication) {
  178. c.Title("admin.auths.edit")
  179. c.PageIs("Admin")
  180. c.PageIs("AdminAuthentications")
  181. c.Data["SMTPAuths"] = models.SMTPAuths
  182. source, err := models.GetLoginSourceByID(c.ParamsInt64(":authid"))
  183. if err != nil {
  184. c.ServerError("GetLoginSourceByID", err)
  185. return
  186. }
  187. c.Data["Source"] = source
  188. c.Data["HasTLS"] = source.HasTLS()
  189. if c.HasError() {
  190. c.Success(AUTH_EDIT)
  191. return
  192. }
  193. var config core.Conversion
  194. switch models.LoginType(f.Type) {
  195. case models.LOGIN_LDAP, models.LOGIN_DLDAP:
  196. config = parseLDAPConfig(f)
  197. case models.LOGIN_SMTP:
  198. config = parseSMTPConfig(f)
  199. case models.LOGIN_PAM:
  200. config = &models.PAMConfig{
  201. ServiceName: f.PAMServiceName,
  202. }
  203. default:
  204. c.Error(400)
  205. return
  206. }
  207. source.Name = f.Name
  208. source.IsActived = f.IsActive
  209. source.IsDefault = f.IsDefault
  210. source.Cfg = config
  211. if err := models.UpdateLoginSource(source); err != nil {
  212. c.ServerError("UpdateLoginSource", err)
  213. return
  214. }
  215. log.Trace("Authentication changed by admin '%s': %d", c.User.Name, source.ID)
  216. c.Flash.Success("Authentication setting has been updated successfully.")
  217. c.Redirect(setting.AppSubURL + "/admin/auths/" + com.ToStr(f.ID))
  218. }
  219. // DeleteAuthSource deletes an auth source
  220. func DeleteAuthSource(c *context.Context) {
  221. source, err := models.GetLoginSourceByID(c.ParamsInt64(":authid"))
  222. if err != nil {
  223. c.ServerError("GetLoginSourceByID", err)
  224. return
  225. }
  226. if err = models.DeleteSource(source); err != nil {
  227. if models.IsErrLoginSourceInUse(err) {
  228. c.Flash.Error("This authentication is still used by some users, please delete or convert these users to another login type first.")
  229. } else {
  230. c.Flash.Error(fmt.Sprintf("DeleteSource: %v", err))
  231. }
  232. c.JSONSuccess(map[string]interface{}{
  233. "redirect": setting.AppSubURL + "/admin/auths/" + c.Params(":authid"),
  234. })
  235. return
  236. }
  237. log.Trace("Authentication deleted by admin(%s): %d", c.User.Name, source.ID)
  238. c.Flash.Success("Authentication has been deleted successfully!")
  239. c.JSONSuccess(map[string]interface{}{
  240. "redirect": setting.AppSubURL + "/admin/auths",
  241. })
  242. }