|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/rand" |
| 6 | + "encoding/hex" |
| 7 | + "fmt" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/urnetwork/server" |
| 11 | + "github.com/urnetwork/server/session" |
| 12 | +) |
| 13 | + |
| 14 | +type CreateApiKeyArgs struct { |
| 15 | + Name string `json:"name"` |
| 16 | +} |
| 17 | + |
| 18 | +type CreateApiKeyError struct { |
| 19 | + Message string `json:"message"` |
| 20 | +} |
| 21 | + |
| 22 | +type CreateApiKeyResult struct { |
| 23 | + Id server.Id `json:"id,omitempty"` |
| 24 | + ApiKey string `json:"api_key,omitempty"` |
| 25 | + Error *CreateApiKeyError `json:"error,omitempty"` |
| 26 | +} |
| 27 | + |
| 28 | +func CreateApiKey(createApiKey *CreateApiKeyArgs, session *session.ClientSession) (result *CreateApiKeyResult, err error) { |
| 29 | + var apiKeyId server.Id |
| 30 | + var apiKey string |
| 31 | + |
| 32 | + server.Tx(session.Ctx, func(tx server.PgTx) { |
| 33 | + apiKeyId = server.NewId() |
| 34 | + |
| 35 | + var keyErr error |
| 36 | + apiKey, keyErr = generateApiKey() |
| 37 | + if keyErr != nil { |
| 38 | + err = keyErr |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + _, err = tx.Exec( |
| 43 | + session.Ctx, |
| 44 | + ` |
| 45 | + INSERT INTO account_api_key |
| 46 | + ( |
| 47 | + api_key_id, |
| 48 | + network_id, |
| 49 | + api_key, |
| 50 | + name |
| 51 | + ) |
| 52 | + VALUES ($1, $2, $3, $4) |
| 53 | + `, |
| 54 | + apiKeyId, |
| 55 | + session.ByJwt.NetworkId, |
| 56 | + apiKey, |
| 57 | + createApiKey.Name, |
| 58 | + ) |
| 59 | + }) |
| 60 | + |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + return &CreateApiKeyResult{ |
| 66 | + Id: apiKeyId, |
| 67 | + ApiKey: apiKey, |
| 68 | + }, nil |
| 69 | +} |
| 70 | + |
| 71 | +func DeleteApiKey(apiKeyId *server.Id, session *session.ClientSession) (err error) { |
| 72 | + server.Tx(session.Ctx, func(tx server.PgTx) { |
| 73 | + _, err = tx.Exec( |
| 74 | + session.Ctx, |
| 75 | + ` |
| 76 | + DELETE FROM account_api_key |
| 77 | + WHERE api_key_id = $1 AND network_id = $2 |
| 78 | + `, |
| 79 | + apiKeyId, |
| 80 | + session.ByJwt.NetworkId, |
| 81 | + ) |
| 82 | + }) |
| 83 | + return |
| 84 | +} |
| 85 | + |
| 86 | +type PublicAccountApiKey struct { |
| 87 | + Id server.Id `json:"id"` |
| 88 | + Name string `json:"name"` |
| 89 | + CreateTime time.Time `json:"create_time"` |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * for dashboard listing of API keys |
| 94 | + */ |
| 95 | +func GetAccountApiKeys(session *session.ClientSession) (apiKeys []*PublicAccountApiKey, err error) { |
| 96 | + server.Tx(session.Ctx, func(tx server.PgTx) { |
| 97 | + result, err := tx.Query( |
| 98 | + session.Ctx, |
| 99 | + ` |
| 100 | + SELECT api_key_id, name, create_time |
| 101 | + FROM account_api_key |
| 102 | + WHERE network_id = $1 |
| 103 | + `, |
| 104 | + session.ByJwt.NetworkId, |
| 105 | + ) |
| 106 | + |
| 107 | + server.WithPgResult(result, err, func() { |
| 108 | + for result.Next() { |
| 109 | + var apiKeyId server.Id |
| 110 | + var name string |
| 111 | + var createTime time.Time |
| 112 | + err = result.Scan(&apiKeyId, &name, &createTime) |
| 113 | + if err != nil { |
| 114 | + return |
| 115 | + } |
| 116 | + apiKeys = append(apiKeys, &PublicAccountApiKey{ |
| 117 | + Id: apiKeyId, |
| 118 | + Name: name, |
| 119 | + CreateTime: createTime, |
| 120 | + }) |
| 121 | + } |
| 122 | + }) |
| 123 | + }) |
| 124 | + return |
| 125 | +} |
| 126 | + |
| 127 | +type GetApiKeyError struct { |
| 128 | + Message string `json:"message"` |
| 129 | +} |
| 130 | + |
| 131 | +type NetworkByApiKey struct { |
| 132 | + NetworkId server.Id |
| 133 | + UserId server.Id |
| 134 | + NetworkName string |
| 135 | +} |
| 136 | + |
| 137 | +func GetNetworkByApiKey(apiKey string, ctx context.Context) *NetworkByApiKey { |
| 138 | + var result *NetworkByApiKey |
| 139 | + |
| 140 | + server.Db(ctx, func(conn server.PgConn) { |
| 141 | + rows, err := conn.Query( |
| 142 | + ctx, |
| 143 | + ` |
| 144 | + SELECT |
| 145 | + network.network_id, |
| 146 | + network.admin_user_id, |
| 147 | + network.network_name |
| 148 | + FROM account_api_key |
| 149 | + JOIN network ON network.network_id = account_api_key.network_id |
| 150 | + WHERE account_api_key.api_key = $1 |
| 151 | + `, |
| 152 | + apiKey, |
| 153 | + ) |
| 154 | + |
| 155 | + server.WithPgResult(rows, err, func() { |
| 156 | + if rows.Next() { |
| 157 | + var r NetworkByApiKey |
| 158 | + server.RaisePgResult(rows.Scan( |
| 159 | + &r.NetworkId, |
| 160 | + &r.UserId, |
| 161 | + &r.NetworkName, |
| 162 | + ), err) |
| 163 | + result = &r |
| 164 | + } |
| 165 | + }) |
| 166 | + }) |
| 167 | + |
| 168 | + return result |
| 169 | +} |
| 170 | + |
| 171 | +func generateApiKey() (string, error) { |
| 172 | + b := make([]byte, 32) |
| 173 | + if _, err := rand.Read(b); err != nil { |
| 174 | + return "", err |
| 175 | + } |
| 176 | + return fmt.Sprintf("urn_%s", hex.EncodeToString(b)), nil |
| 177 | +} |
| 178 | + |
| 179 | +func init() { |
| 180 | + session.ApiKeyLookup = func(ctx context.Context, apiKey string) (server.Id, server.Id, string, bool) { |
| 181 | + n := GetNetworkByApiKey(apiKey, ctx) |
| 182 | + if n == nil { |
| 183 | + return server.Id{}, server.Id{}, "", false |
| 184 | + } |
| 185 | + return n.NetworkId, n.UserId, n.NetworkName, true |
| 186 | + } |
| 187 | +} |
0 commit comments