oauth.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package integrations
  2. import (
  3. "gorm.io/gorm"
  4. )
  5. // OAuthIntegrationClient is the name of an OAuth mechanism client
  6. type OAuthIntegrationClient string
  7. // The supported oauth mechanism clients
  8. const (
  9. OAuthGithub OAuthIntegrationClient = "github"
  10. OAuthDigitalOcean OAuthIntegrationClient = "do"
  11. OAuthGoogle OAuthIntegrationClient = "google"
  12. )
  13. // OAuthIntegration is an auth mechanism that uses oauth
  14. // https://tools.ietf.org/html/rfc6749
  15. type OAuthIntegration struct {
  16. gorm.Model
  17. // The name of the auth mechanism
  18. Client OAuthIntegrationClient `json:"client"`
  19. // The id of the user that linked this auth mechanism
  20. UserID uint `json:"user_id"`
  21. // The project that this integration belongs to
  22. ProjectID uint `json:"project_id"`
  23. // ------------------------------------------------------------------
  24. // All fields encrypted before storage.
  25. // ------------------------------------------------------------------
  26. // The ID issued to the client
  27. ClientID []byte `json:"client-id"`
  28. // The end-users's access token
  29. AccessToken []byte `json:"access-token"`
  30. // The end-user's refresh token
  31. RefreshToken []byte `json:"refresh-token"`
  32. }
  33. // OAuthIntegrationExternal is an OAuthIntegration to be shared over REST
  34. type OAuthIntegrationExternal struct {
  35. ID uint `json:"id"`
  36. // The name of the auth mechanism
  37. Client OAuthIntegrationClient `json:"client"`
  38. // The id of the user that linked this auth mechanism
  39. UserID uint `json:"user_id"`
  40. // The project that this integration belongs to
  41. ProjectID uint `json:"project_id"`
  42. }
  43. // Externalize generates an external KubeIntegration to be shared over REST
  44. func (o *OAuthIntegration) Externalize() *OAuthIntegrationExternal {
  45. return &OAuthIntegrationExternal{
  46. ID: o.ID,
  47. Client: o.Client,
  48. UserID: o.UserID,
  49. ProjectID: o.ProjectID,
  50. }
  51. }
  52. // ToProjectIntegration converts an oauth integration to a project integration
  53. func (o *OAuthIntegration) ToProjectIntegration(
  54. category string,
  55. service IntegrationService,
  56. ) *ProjectIntegration {
  57. return &ProjectIntegration{
  58. ID: o.ID,
  59. ProjectID: o.ProjectID,
  60. AuthMechanism: "oauth",
  61. Category: category,
  62. Service: service,
  63. }
  64. }