oauth.go 2.0 KB

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