auths.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. "github.com/Unknwon/com"
  8. "github.com/go-xorm/core"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/auth"
  11. "github.com/gogits/gogs/modules/auth/ldap"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/context"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. const (
  18. AUTHS base.TplName = "admin/auth/list"
  19. AUTH_NEW base.TplName = "admin/auth/new"
  20. AUTH_EDIT base.TplName = "admin/auth/edit"
  21. )
  22. func Authentications(ctx *context.Context) {
  23. ctx.Data["Title"] = ctx.Tr("admin.authentication")
  24. ctx.Data["PageIsAdmin"] = true
  25. ctx.Data["PageIsAdminAuthentications"] = true
  26. var err error
  27. ctx.Data["Sources"], err = models.LoginSources()
  28. if err != nil {
  29. ctx.Handle(500, "LoginSources", err)
  30. return
  31. }
  32. ctx.Data["Total"] = models.CountLoginSources()
  33. ctx.HTML(200, AUTHS)
  34. }
  35. type dropdownItem struct {
  36. Name string
  37. Type interface{}
  38. }
  39. var (
  40. authSources = []dropdownItem{
  41. {models.LoginNames[models.LOGIN_LDAP], models.LOGIN_LDAP},
  42. {models.LoginNames[models.LOGIN_DLDAP], models.LOGIN_DLDAP},
  43. {models.LoginNames[models.LOGIN_SMTP], models.LOGIN_SMTP},
  44. {models.LoginNames[models.LOGIN_PAM], models.LOGIN_PAM},
  45. }
  46. securityProtocols = []dropdownItem{
  47. {models.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_UNENCRYPTED], ldap.SECURITY_PROTOCOL_UNENCRYPTED},
  48. {models.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_LDAPS], ldap.SECURITY_PROTOCOL_LDAPS},
  49. {models.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_START_TLS], ldap.SECURITY_PROTOCOL_START_TLS},
  50. }
  51. )
  52. func NewAuthSource(ctx *context.Context) {
  53. ctx.Data["Title"] = ctx.Tr("admin.auths.new")
  54. ctx.Data["PageIsAdmin"] = true
  55. ctx.Data["PageIsAdminAuthentications"] = true
  56. ctx.Data["type"] = models.LOGIN_LDAP
  57. ctx.Data["CurrentTypeName"] = models.LoginNames[models.LOGIN_LDAP]
  58. ctx.Data["CurrentSecurityProtocol"] = models.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_UNENCRYPTED]
  59. ctx.Data["smtp_auth"] = "PLAIN"
  60. ctx.Data["is_active"] = true
  61. ctx.Data["AuthSources"] = authSources
  62. ctx.Data["SecurityProtocols"] = securityProtocols
  63. ctx.Data["SMTPAuths"] = models.SMTPAuths
  64. ctx.HTML(200, AUTH_NEW)
  65. }
  66. func parseLDAPConfig(form auth.AuthenticationForm) *models.LDAPConfig {
  67. return &models.LDAPConfig{
  68. Source: &ldap.Source{
  69. Name: form.Name,
  70. Host: form.Host,
  71. Port: form.Port,
  72. SecurityProtocol: ldap.SecurityProtocol(form.SecurityProtocol),
  73. SkipVerify: form.SkipVerify,
  74. BindDN: form.BindDN,
  75. UserDN: form.UserDN,
  76. BindPassword: form.BindPassword,
  77. UserBase: form.UserBase,
  78. AttributeUsername: form.AttributeUsername,
  79. AttributeName: form.AttributeName,
  80. AttributeSurname: form.AttributeSurname,
  81. AttributeMail: form.AttributeMail,
  82. AttributesInBind: form.AttributesInBind,
  83. Filter: form.Filter,
  84. AdminFilter: form.AdminFilter,
  85. Enabled: true,
  86. },
  87. }
  88. }
  89. func parseSMTPConfig(form auth.AuthenticationForm) *models.SMTPConfig {
  90. return &models.SMTPConfig{
  91. Auth: form.SMTPAuth,
  92. Host: form.SMTPHost,
  93. Port: form.SMTPPort,
  94. AllowedDomains: form.AllowedDomains,
  95. TLS: form.TLS,
  96. SkipVerify: form.SkipVerify,
  97. }
  98. }
  99. func NewAuthSourcePost(ctx *context.Context, form auth.AuthenticationForm) {
  100. ctx.Data["Title"] = ctx.Tr("admin.auths.new")
  101. ctx.Data["PageIsAdmin"] = true
  102. ctx.Data["PageIsAdminAuthentications"] = true
  103. ctx.Data["CurrentTypeName"] = models.LoginNames[models.LoginType(form.Type)]
  104. ctx.Data["CurrentSecurityProtocol"] = models.SecurityProtocolNames[ldap.SecurityProtocol(form.SecurityProtocol)]
  105. ctx.Data["AuthSources"] = authSources
  106. ctx.Data["SecurityProtocols"] = securityProtocols
  107. ctx.Data["SMTPAuths"] = models.SMTPAuths
  108. hasTLS := false
  109. var config core.Conversion
  110. switch models.LoginType(form.Type) {
  111. case models.LOGIN_LDAP, models.LOGIN_DLDAP:
  112. config = parseLDAPConfig(form)
  113. hasTLS = ldap.SecurityProtocol(form.SecurityProtocol) > ldap.SECURITY_PROTOCOL_UNENCRYPTED
  114. case models.LOGIN_SMTP:
  115. config = parseSMTPConfig(form)
  116. hasTLS = true
  117. case models.LOGIN_PAM:
  118. config = &models.PAMConfig{
  119. ServiceName: form.PAMServiceName,
  120. }
  121. default:
  122. ctx.Error(400)
  123. return
  124. }
  125. ctx.Data["HasTLS"] = hasTLS
  126. if ctx.HasError() {
  127. ctx.HTML(200, AUTH_NEW)
  128. return
  129. }
  130. if err := models.CreateSource(&models.LoginSource{
  131. Type: models.LoginType(form.Type),
  132. Name: form.Name,
  133. IsActived: form.IsActive,
  134. Cfg: config,
  135. }); err != nil {
  136. ctx.Handle(500, "CreateSource", err)
  137. return
  138. }
  139. log.Trace("Authentication created by admin(%s): %s", ctx.User.Name, form.Name)
  140. ctx.Flash.Success(ctx.Tr("admin.auths.new_success", form.Name))
  141. ctx.Redirect(setting.AppSubUrl + "/admin/auths")
  142. }
  143. func EditAuthSource(ctx *context.Context) {
  144. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  145. ctx.Data["PageIsAdmin"] = true
  146. ctx.Data["PageIsAdminAuthentications"] = true
  147. ctx.Data["SecurityProtocols"] = securityProtocols
  148. ctx.Data["SMTPAuths"] = models.SMTPAuths
  149. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  150. if err != nil {
  151. ctx.Handle(500, "GetLoginSourceByID", err)
  152. return
  153. }
  154. ctx.Data["Source"] = source
  155. ctx.Data["HasTLS"] = source.HasTLS()
  156. ctx.HTML(200, AUTH_EDIT)
  157. }
  158. func EditAuthSourcePost(ctx *context.Context, form auth.AuthenticationForm) {
  159. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  160. ctx.Data["PageIsAdmin"] = true
  161. ctx.Data["PageIsAdminAuthentications"] = true
  162. ctx.Data["SMTPAuths"] = models.SMTPAuths
  163. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  164. if err != nil {
  165. ctx.Handle(500, "GetLoginSourceByID", err)
  166. return
  167. }
  168. ctx.Data["Source"] = source
  169. ctx.Data["HasTLS"] = source.HasTLS()
  170. if ctx.HasError() {
  171. ctx.HTML(200, AUTH_EDIT)
  172. return
  173. }
  174. var config core.Conversion
  175. switch models.LoginType(form.Type) {
  176. case models.LOGIN_LDAP, models.LOGIN_DLDAP:
  177. config = parseLDAPConfig(form)
  178. case models.LOGIN_SMTP:
  179. config = parseSMTPConfig(form)
  180. case models.LOGIN_PAM:
  181. config = &models.PAMConfig{
  182. ServiceName: form.PAMServiceName,
  183. }
  184. default:
  185. ctx.Error(400)
  186. return
  187. }
  188. source.Name = form.Name
  189. source.IsActived = form.IsActive
  190. source.Cfg = config
  191. if err := models.UpdateSource(source); err != nil {
  192. ctx.Handle(500, "UpdateSource", err)
  193. return
  194. }
  195. log.Trace("Authentication changed by admin(%s): %s", ctx.User.Name, source.ID)
  196. ctx.Flash.Success(ctx.Tr("admin.auths.update_success"))
  197. ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + com.ToStr(form.ID))
  198. }
  199. func DeleteAuthSource(ctx *context.Context) {
  200. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  201. if err != nil {
  202. ctx.Handle(500, "GetLoginSourceByID", err)
  203. return
  204. }
  205. if err = models.DeleteSource(source); err != nil {
  206. switch err {
  207. case models.ErrAuthenticationUserUsed:
  208. ctx.Flash.Error(ctx.Tr("admin.auths.still_in_used"))
  209. default:
  210. ctx.Flash.Error(fmt.Sprintf("DeleteSource: %v", err))
  211. }
  212. ctx.JSON(200, map[string]interface{}{
  213. "redirect": setting.AppSubUrl + "/admin/auths/" + ctx.Params(":authid"),
  214. })
  215. return
  216. }
  217. log.Trace("Authentication deleted by admin(%s): %d", ctx.User.Name, source.ID)
  218. ctx.Flash.Success(ctx.Tr("admin.auths.deletion_success"))
  219. ctx.JSON(200, map[string]interface{}{
  220. "redirect": setting.AppSubUrl + "/admin/auths",
  221. })
  222. }