oauth.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package integrations
  2. import (
  3. "gorm.io/gorm"
  4. "time"
  5. )
  6. // OAuthIntegrationClient is the name of an OAuth mechanism client
  7. type OAuthIntegrationClient string
  8. // The supported oauth mechanism clients
  9. const (
  10. OAuthGithub OAuthIntegrationClient = "github"
  11. OAuthDigitalOcean OAuthIntegrationClient = "do"
  12. OAuthGoogle OAuthIntegrationClient = "google"
  13. )
  14. // SharedOAuthModel stores general fields needed for OAuth Integration
  15. type SharedOAuthModel struct {
  16. // The ID issued to the client
  17. ClientID []byte `json:"client-id"`
  18. // The end-users's access token
  19. AccessToken []byte `json:"access-token"`
  20. // The end-user's refresh token
  21. RefreshToken []byte `json:"refresh-token"`
  22. // Time token expires and needs to be refreshed.
  23. // If 0, token will never refresh
  24. Expiry time.Time
  25. }
  26. // OAuthIntegration is an auth mechanism that uses oauth
  27. // https://tools.ietf.org/html/rfc6749
  28. type OAuthIntegration struct {
  29. gorm.Model
  30. SharedOAuthModel
  31. // The name of the auth mechanism
  32. Client OAuthIntegrationClient `json:"client"`
  33. // The id of the user that linked this auth mechanism
  34. UserID uint `json:"user_id"`
  35. // The project that this integration belongs to
  36. ProjectID uint `json:"project_id"`
  37. // ------------------------------------------------------------------
  38. // All fields encrypted before storage.
  39. // ------------------------------------------------------------------
  40. }
  41. // GithubAppOAuthIntegration is the model used for storing github app oauth data
  42. // Unlike the above, this model is tied to a specific user, not a project
  43. type GithubAppOAuthIntegration struct {
  44. gorm.Model
  45. SharedOAuthModel
  46. // The id of the user that linked this auth mechanism
  47. UserID uint `json:"user_id"`
  48. }
  49. // OAuthIntegrationExternal is an OAuthIntegration to be shared over REST
  50. type OAuthIntegrationExternal struct {
  51. ID uint `json:"id"`
  52. // The name of the auth mechanism
  53. Client OAuthIntegrationClient `json:"client"`
  54. // The id of the user that linked this auth mechanism
  55. UserID uint `json:"user_id"`
  56. // The project that this integration belongs to
  57. ProjectID uint `json:"project_id"`
  58. }
  59. // Externalize generates an external KubeIntegration to be shared over REST
  60. func (o *OAuthIntegration) Externalize() *OAuthIntegrationExternal {
  61. return &OAuthIntegrationExternal{
  62. ID: o.ID,
  63. Client: o.Client,
  64. UserID: o.UserID,
  65. ProjectID: o.ProjectID,
  66. }
  67. }
  68. // ToProjectIntegration converts an oauth integration to a project integration
  69. func (o *OAuthIntegration) ToProjectIntegration(
  70. category string,
  71. service IntegrationService,
  72. ) *ProjectIntegration {
  73. return &ProjectIntegration{
  74. ID: o.ID,
  75. ProjectID: o.ProjectID,
  76. AuthMechanism: "oauth",
  77. Category: category,
  78. Service: service,
  79. }
  80. }