oauth.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // SharedOAuthModel stores general fields needed for OAuth Integration
  14. type SharedOAuthModel struct {
  15. // The ID issued to the client
  16. ClientID []byte `json:"client-id"`
  17. // The end-users's access token
  18. AccessToken []byte `json:"access-token"`
  19. // The end-user's refresh token
  20. RefreshToken []byte `json:"refresh-token"`
  21. }
  22. // OAuthIntegration is an auth mechanism that uses oauth
  23. // https://tools.ietf.org/html/rfc6749
  24. type OAuthIntegration struct {
  25. gorm.Model
  26. SharedOAuthModel
  27. // The name of the auth mechanism
  28. Client OAuthIntegrationClient `json:"client"`
  29. // The id of the user that linked this auth mechanism
  30. UserID uint `json:"user_id"`
  31. // The project that this integration belongs to
  32. ProjectID uint `json:"project_id"`
  33. // ------------------------------------------------------------------
  34. // All fields encrypted before storage.
  35. // ------------------------------------------------------------------
  36. }
  37. // GithubAppOAuthIntegration is the model used for storing github app oauth data
  38. // Unlike the above, this model is tied to a specific user, not a project
  39. type GithubAppOAuthIntegration struct {
  40. gorm.Model
  41. SharedOAuthModel
  42. // The id of the user that linked this auth mechanism
  43. UserID uint `json:"user_id"`
  44. }
  45. // OAuthIntegrationExternal is an OAuthIntegration to be shared over REST
  46. type OAuthIntegrationExternal struct {
  47. ID uint `json:"id"`
  48. // The name of the auth mechanism
  49. Client OAuthIntegrationClient `json:"client"`
  50. // The id of the user that linked this auth mechanism
  51. UserID uint `json:"user_id"`
  52. // The project that this integration belongs to
  53. ProjectID uint `json:"project_id"`
  54. }
  55. // Externalize generates an external KubeIntegration to be shared over REST
  56. func (o *OAuthIntegration) Externalize() *OAuthIntegrationExternal {
  57. return &OAuthIntegrationExternal{
  58. ID: o.ID,
  59. Client: o.Client,
  60. UserID: o.UserID,
  61. ProjectID: o.ProjectID,
  62. }
  63. }
  64. // ToProjectIntegration converts an oauth integration to a project integration
  65. func (o *OAuthIntegration) ToProjectIntegration(
  66. category string,
  67. service IntegrationService,
  68. ) *ProjectIntegration {
  69. return &ProjectIntegration{
  70. ID: o.ID,
  71. ProjectID: o.ProjectID,
  72. AuthMechanism: "oauth",
  73. Category: category,
  74. Service: service,
  75. }
  76. }