oauth.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package integrations
  2. import (
  3. "github.com/porter-dev/porter/api/types"
  4. "gorm.io/gorm"
  5. "time"
  6. )
  7. // SharedOAuthModel stores general fields needed for OAuth Integration
  8. type SharedOAuthModel struct {
  9. // The ID issued to the client
  10. ClientID []byte `json:"client-id"`
  11. // The end-users's access token
  12. AccessToken []byte `json:"access-token"`
  13. // The end-user's refresh token
  14. RefreshToken []byte `json:"refresh-token"`
  15. // Time token expires and needs to be refreshed.
  16. // If 0, token will never refresh
  17. Expiry time.Time
  18. }
  19. // OAuthIntegration is an auth mechanism that uses oauth
  20. // https://tools.ietf.org/html/rfc6749
  21. type OAuthIntegration struct {
  22. gorm.Model
  23. SharedOAuthModel
  24. // The name of the auth mechanism
  25. Client types.OAuthIntegrationClient `json:"client"`
  26. // The id of the user that linked this auth mechanism
  27. UserID uint `json:"user_id"`
  28. // The project that this integration belongs to
  29. ProjectID uint `json:"project_id"`
  30. // ------------------------------------------------------------------
  31. // All fields encrypted before storage.
  32. // ------------------------------------------------------------------
  33. }
  34. // GithubAppOAuthIntegration is the model used for storing github app oauth data
  35. // Unlike the above, this model is tied to a specific user, not a project
  36. type GithubAppOAuthIntegration struct {
  37. gorm.Model
  38. SharedOAuthModel
  39. // The id of the user that linked this auth mechanism
  40. UserID uint `json:"user_id"`
  41. }
  42. // ToOAuthIntegrationType generates an external OAuthIntegration to be shared over REST
  43. func (o *OAuthIntegration) ToOAuthIntegrationType() *types.OAuthIntegration {
  44. return &types.OAuthIntegration{
  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. }