auths.go 7.5 KB

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