oauth.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package integrations
  2. import (
  3. "time"
  4. "github.com/porter-dev/porter/api/types"
  5. "gorm.io/gorm"
  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. }