DeleteExpiredOTPs deletes the expired OTPs for all auth collections.
()
| 83 | |
| 84 | // DeleteExpiredOTPs deletes the expired OTPs for all auth collections. |
| 85 | func (app *BaseApp) DeleteExpiredOTPs() error { |
| 86 | authCollections, err := app.FindAllCollections(CollectionTypeAuth) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | |
| 91 | // note: perform even if OTP is disabled to ensure that there are no dangling old records |
| 92 | for _, collection := range authCollections { |
| 93 | minValidDate, err := types.ParseDateTime(time.Now().Add(-1 * collection.OTP.DurationTime())) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | items := []*Record{} |
| 99 | |
| 100 | err = app.RecordQuery(CollectionNameOTPs). |
| 101 | AndWhere(dbx.HashExp{"collectionRef": collection.Id}). |
| 102 | AndWhere(dbx.NewExp("[[created]] < {:date}", dbx.Params{"date": minValidDate})). |
| 103 | All(&items) |
| 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | |
| 108 | for _, item := range items { |
| 109 | err = app.Delete(item) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return nil |
| 117 | } |
no test coverage detected