pending_auth_session.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package schema
  2. import (
  3. "fmt"
  4. "github.com/Wei-Shaw/sub2api/ent/schema/mixins"
  5. "entgo.io/ent"
  6. "entgo.io/ent/dialect"
  7. "entgo.io/ent/dialect/entsql"
  8. "entgo.io/ent/schema"
  9. "entgo.io/ent/schema/edge"
  10. "entgo.io/ent/schema/field"
  11. "entgo.io/ent/schema/index"
  12. )
  13. var pendingAuthIntents = map[string]struct{}{
  14. "login": {},
  15. "bind_current_user": {},
  16. "adopt_existing_user_by_email": {},
  17. }
  18. func validatePendingAuthIntent(value string) error {
  19. if _, ok := pendingAuthIntents[value]; ok {
  20. return nil
  21. }
  22. return fmt.Errorf("invalid pending auth intent %q", value)
  23. }
  24. // PendingAuthSession stores a short-lived post-auth decision session.
  25. type PendingAuthSession struct {
  26. ent.Schema
  27. }
  28. func (PendingAuthSession) Annotations() []schema.Annotation {
  29. return []schema.Annotation{
  30. entsql.Annotation{Table: "pending_auth_sessions"},
  31. }
  32. }
  33. func (PendingAuthSession) Mixin() []ent.Mixin {
  34. return []ent.Mixin{
  35. mixins.TimeMixin{},
  36. }
  37. }
  38. func (PendingAuthSession) Fields() []ent.Field {
  39. return []ent.Field{
  40. field.String("session_token").
  41. MaxLen(255).
  42. NotEmpty(),
  43. field.String("intent").
  44. MaxLen(40).
  45. NotEmpty().
  46. Validate(validatePendingAuthIntent),
  47. field.String("provider_type").
  48. MaxLen(20).
  49. NotEmpty().
  50. Validate(validateAuthProviderType),
  51. field.String("provider_key").
  52. NotEmpty().
  53. SchemaType(map[string]string{dialect.Postgres: "text"}),
  54. field.String("provider_subject").
  55. NotEmpty().
  56. SchemaType(map[string]string{dialect.Postgres: "text"}),
  57. field.Int64("target_user_id").
  58. Optional().
  59. Nillable(),
  60. field.String("redirect_to").
  61. Default("").
  62. SchemaType(map[string]string{dialect.Postgres: "text"}),
  63. field.String("resolved_email").
  64. Default("").
  65. SchemaType(map[string]string{dialect.Postgres: "text"}),
  66. field.String("registration_password_hash").
  67. Default("").
  68. SchemaType(map[string]string{dialect.Postgres: "text"}),
  69. field.JSON("upstream_identity_claims", map[string]any{}).
  70. Default(func() map[string]any { return map[string]any{} }).
  71. SchemaType(map[string]string{dialect.Postgres: "jsonb"}),
  72. field.JSON("local_flow_state", map[string]any{}).
  73. Default(func() map[string]any { return map[string]any{} }).
  74. SchemaType(map[string]string{dialect.Postgres: "jsonb"}),
  75. field.String("browser_session_key").
  76. Default("").
  77. SchemaType(map[string]string{dialect.Postgres: "text"}),
  78. field.String("completion_code_hash").
  79. Default("").
  80. SchemaType(map[string]string{dialect.Postgres: "text"}),
  81. field.Time("completion_code_expires_at").
  82. Optional().
  83. Nillable().
  84. SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
  85. field.Time("email_verified_at").
  86. Optional().
  87. Nillable().
  88. SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
  89. field.Time("password_verified_at").
  90. Optional().
  91. Nillable().
  92. SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
  93. field.Time("totp_verified_at").
  94. Optional().
  95. Nillable().
  96. SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
  97. field.Time("expires_at").
  98. SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
  99. field.Time("consumed_at").
  100. Optional().
  101. Nillable().
  102. SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
  103. }
  104. }
  105. func (PendingAuthSession) Edges() []ent.Edge {
  106. return []ent.Edge{
  107. edge.From("target_user", User.Type).
  108. Ref("pending_auth_sessions").
  109. Field("target_user_id").
  110. Unique(),
  111. edge.To("adoption_decision", IdentityAdoptionDecision.Type).
  112. Annotations(entsql.OnDelete(entsql.Cascade)).
  113. Unique(),
  114. }
  115. }
  116. func (PendingAuthSession) Indexes() []ent.Index {
  117. return []ent.Index{
  118. index.Fields("session_token").Unique(),
  119. index.Fields("target_user_id"),
  120. index.Fields("expires_at"),
  121. index.Fields("provider_type", "provider_key", "provider_subject"),
  122. index.Fields("completion_code_hash"),
  123. }
  124. }