~sircmpwn/meta.sr.ht

ref: 0.56.17 meta.sr.ht/api/graph/model/oauthgrant.go -rw-r--r-- 1.4 KiB
b1deacf0Nicolas Schodet Fix field name when validating the PGP key in RegisterAccount 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package model

import (
	"context"
	"database/sql"
	"time"

	sq "github.com/Masterminds/squirrel"

	"git.sr.ht/~sircmpwn/core-go/database"
)

type OAuthGrant struct {
	ID        int       `json:"id"`
	Issued    time.Time `json:"issued"`
	Expires   time.Time `json:"expires"`
	TokenHash string    `json:"token_hash"`

	ClientID int `json:"client"`

	alias  string
	fields *database.ModelFields
}

func (og *OAuthGrant) As(alias string) *OAuthGrant {
	og.alias = alias
	return og
}

func (o *OAuthGrant) Alias() string {
	return o.alias
}

func (o *OAuthGrant) Table() string {
	return "oauth2_grant"
}

func (o *OAuthGrant) Fields() *database.ModelFields {
	if o.fields != nil {
		return o.fields
	}
	o.fields = &database.ModelFields{
		Fields: []*database.FieldMap{
			{ "id", "id", &o.ID },
			{ "issued", "issued", &o.Issued },
			{ "expires", "expires", &o.Expires },
			{ "token_hash", "tokenHash", &o.TokenHash },

			// Always fetch:
			{ "id", "", &o.ID },
			{ "client_id", "", &o.ClientID },
		},
	}
	return o.fields
}

func (og *OAuthGrant) Query(ctx context.Context, runner sq.BaseRunner,
	q sq.SelectBuilder) []*OAuthGrant {

	var (
		err  error
		rows *sql.Rows
	)

	if rows, err = q.RunWith(runner).QueryContext(ctx); err != nil {
		panic(err)
	}
	defer rows.Close()

	var grants []*OAuthGrant
	for rows.Next() {
		var og OAuthGrant
		if err := rows.Scan(database.Scan(ctx, &og)...); err != nil {
			panic(err)
		}
		grants = append(grants, &og)
	}

	return grants
}