auths.go 7.5 KB

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