oauth.go 2.1 KB

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