GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(api): Add permission management API · DropKit/DropKit-Adapter@e59a071

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • constants
    • constant.go
  • controller
    • controller.go
  • router
    • routers.go
  • services
    • database.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎constants/constant.go‎

Copy file name to clipboardExpand all lines: constants/constant.go
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ type Query_Response struct {
2121
Response interface{}
2222
Hash string
2323
}
24+
25+
type Auth_Response struct {
26+
Result string
27+
}
Collapse file

‎controller/controller.go‎

Copy file name to clipboardExpand all lines: controller/controller.go
+86-16Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/spf13/viper"
1616
)
1717

18-
func Create(w http.ResponseWriter, r *http.Request) {
18+
func SQLCreate(w http.ResponseWriter, r *http.Request) {
1919
metaTableAddress := viper.GetString(`DROPKIT.METATABLE`)
2020
authorityAddr := viper.GetString(`DROPKIT.AUTHORITY`)
2121

@@ -26,25 +26,25 @@ func Create(w http.ResponseWriter, r *http.Request) {
2626
var newStatement constants.DB_Statement
2727
_ = json.Unmarshal(body, &newStatement)
2828
sqlCommand := newStatement.Statement
29-
privatekeyHex := newStatement.PrivateKey
30-
callerAddress := account.PrivateKeyToPublicKey(privatekeyHex)
29+
callerPriavteKey := newStatement.PrivateKey
30+
callerAddress := account.PrivateKeyToPublicKey(callerPriavteKey)
3131

3232
tableName := parser.GetTableName(sqlCommand)
3333
_, tableAddress := account.GenerateWallet()
3434

35-
services.AddMetaTable(tableName, tableAddress, metaTableAddress, privatekeyHex)
36-
services.GrantAuthority(authorityAddr, privatekeyHex, tableName, callerAddress)
35+
services.AddMetaTable(tableName, tableAddress, metaTableAddress, callerPriavteKey)
36+
services.GrantAuthority(authorityAddr, callerPriavteKey, tableName, callerAddress)
3737
services.Exec(sqlCommand)
3838

39-
aduitTransactionHash := transaction.SendRawTransaction(tableAddress, sqlCommand, 0, privatekeyHex)
39+
aduitTransactionHash := transaction.SendRawTransaction(tableAddress, sqlCommand, 0, callerPriavteKey)
4040

4141
defer r.Body.Close()
4242
response := constants.Exec_Response{"200", aduitTransactionHash}
4343

4444
services.ResponseWithJson(w, http.StatusOK, response)
4545
}
4646

47-
func Insert(w http.ResponseWriter, r *http.Request) {
47+
func SQLInsert(w http.ResponseWriter, r *http.Request) {
4848
metaTableAddress := viper.GetString(`DROPKIT.METATABLE`)
4949
authorityAddr := viper.GetString(`DROPKIT.AUTHORITY`)
5050

@@ -56,17 +56,17 @@ func Insert(w http.ResponseWriter, r *http.Request) {
5656
var newStatement constants.DB_Statement
5757
_ = json.Unmarshal(body, &newStatement)
5858
sqlCommand := newStatement.Statement
59-
privatekeyHex := newStatement.PrivateKey
60-
callerAddress := account.PrivateKeyToPublicKey(privatekeyHex)
59+
callerPriavteKey := newStatement.PrivateKey
60+
callerAddress := account.PrivateKeyToPublicKey(callerPriavteKey)
6161

6262
tableName := parser.GetTableName(sqlCommand)
6363
tableAddress := services.GetMetaTable(tableName, metaTableAddress)
64-
authority := services.VerifyAuthority(authorityAddr, privatekeyHex, tableName, callerAddress)
64+
authority := services.VerifyAuthority(authorityAddr, callerPriavteKey, tableName, callerAddress)
6565

6666
switch authority {
6767
case true:
6868
services.Exec(sqlCommand)
69-
aduitTransactionHash := transaction.SendRawTransaction(tableAddress, sqlCommand, 0, privatekeyHex)
69+
aduitTransactionHash := transaction.SendRawTransaction(tableAddress, sqlCommand, 0, callerPriavteKey)
7070
defer r.Body.Close()
7171
response := constants.Exec_Response{"200", aduitTransactionHash}
7272
services.ResponseWithJson(w, http.StatusOK, response)
@@ -77,7 +77,7 @@ func Insert(w http.ResponseWriter, r *http.Request) {
7777
}
7878
}
7979

80-
func Select(w http.ResponseWriter, r *http.Request) {
80+
func SQLSelect(w http.ResponseWriter, r *http.Request) {
8181
metaTableAddress := viper.GetString(`DROPKIT.METATABLE`)
8282
authorityAddr := viper.GetString(`DROPKIT.AUTHORITY`)
8383

@@ -89,17 +89,17 @@ func Select(w http.ResponseWriter, r *http.Request) {
8989
var newStatement constants.DB_Statement
9090
_ = json.Unmarshal(body, &newStatement)
9191
sqlCommand := newStatement.Statement
92-
privatekeyHex := newStatement.PrivateKey
93-
callerAddress := account.PrivateKeyToPublicKey(privatekeyHex)
92+
callerPriavteKey := newStatement.PrivateKey
93+
callerAddress := account.PrivateKeyToPublicKey(callerPriavteKey)
9494

9595
tableName := parser.GetTableName(sqlCommand)
9696
tableAddress := services.GetMetaTable(tableName, metaTableAddress)
97-
authority := services.VerifyAuthority(authorityAddr, privatekeyHex, tableName, callerAddress)
97+
authority := services.VerifyAuthority(authorityAddr, callerPriavteKey, tableName, callerAddress)
9898

9999
switch authority {
100100
case true:
101101
metadata, _ := services.Query(sqlCommand)
102-
aduitTransactionHash := transaction.SendRawTransaction(tableAddress, sqlCommand, 0, privatekeyHex)
102+
aduitTransactionHash := transaction.SendRawTransaction(tableAddress, sqlCommand, 0, callerPriavteKey)
103103
defer r.Body.Close()
104104
response := constants.Query_Response{"200", metadata, aduitTransactionHash}
105105
services.ResponseWithJson(w, http.StatusOK, response)
@@ -109,3 +109,73 @@ func Select(w http.ResponseWriter, r *http.Request) {
109109
services.ResponseWithJson(w, http.StatusUnauthorized, response)
110110
}
111111
}
112+
113+
func AuthGrant(w http.ResponseWriter, r *http.Request) {
114+
authorityAddr := viper.GetString(`DROPKIT.AUTHORITY`)
115+
116+
body, err := ioutil.ReadAll(r.Body)
117+
if err != nil {
118+
fmt.Println(err)
119+
}
120+
121+
var newStatement constants.Auth_Statement
122+
_ = json.Unmarshal(body, &newStatement)
123+
callerPriavteKey := newStatement.PrivateKey
124+
grantUser := newStatement.UserName
125+
grantTable := newStatement.TableName
126+
127+
services.GrantAuthority(authorityAddr, callerPriavteKey, grantTable, grantUser)
128+
129+
defer r.Body.Close()
130+
response := constants.Auth_Response{"200"}
131+
services.ResponseWithJson(w, http.StatusOK, response)
132+
}
133+
134+
func AuthRevoke(w http.ResponseWriter, r *http.Request) {
135+
authorityAddr := viper.GetString(`DROPKIT.AUTHORITY`)
136+
137+
body, err := ioutil.ReadAll(r.Body)
138+
if err != nil {
139+
fmt.Println(err)
140+
}
141+
142+
var newStatement constants.Auth_Statement
143+
_ = json.Unmarshal(body, &newStatement)
144+
callerPriavteKey := newStatement.PrivateKey
145+
revokeUser := newStatement.UserName
146+
revokeTable := newStatement.TableName
147+
148+
services.RevokeAuthority(authorityAddr, callerPriavteKey, revokeTable, revokeUser)
149+
150+
defer r.Body.Close()
151+
response := constants.Auth_Response{"200"}
152+
services.ResponseWithJson(w, http.StatusOK, response)
153+
}
154+
155+
func AuthVerify(w http.ResponseWriter, r *http.Request) {
156+
authorityAddr := viper.GetString(`DROPKIT.AUTHORITY`)
157+
158+
body, err := ioutil.ReadAll(r.Body)
159+
if err != nil {
160+
fmt.Println(err)
161+
}
162+
163+
var newStatement constants.Auth_Statement
164+
_ = json.Unmarshal(body, &newStatement)
165+
callerPriavteKey := newStatement.PrivateKey
166+
checkUser := newStatement.UserName
167+
checkTable := newStatement.TableName
168+
169+
authority := services.VerifyAuthority(authorityAddr, callerPriavteKey, checkTable, checkUser)
170+
171+
switch authority {
172+
case true:
173+
defer r.Body.Close()
174+
response := constants.Auth_Response{"200"}
175+
services.ResponseWithJson(w, http.StatusOK, response)
176+
case false:
177+
defer r.Body.Close()
178+
response := constants.Auth_Response{"401"}
179+
services.ResponseWithJson(w, http.StatusUnauthorized, response)
180+
}
181+
}
Collapse file

‎router/routers.go‎

Copy file name to clipboardExpand all lines: router/routers.go
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ type Route struct {
1818
var routes []Route
1919

2020
func init() {
21-
register("POST", "/api/db/create", controller.Create, nil)
22-
register("POST", "/api/db/insert", controller.Insert, nil)
23-
register("POST", "/api/db/select", controller.Select, nil)
21+
register("POST", "/api/db/create", controller.SQLCreate, nil)
22+
register("POST", "/api/db/insert", controller.SQLInsert, nil)
23+
register("POST", "/api/db/select", controller.SQLSelect, nil)
2424

25+
register("POST", "/api/auth/grant", controller.AuthGrant, nil)
26+
register("POST", "/api/auth/revoke", controller.AuthRevoke, nil)
27+
register("POST", "/api/auth/verify", controller.AuthVerify, nil)
2528
}
2629

2730
func NewRouter() *mux.Router {
Collapse file

‎services/database.go‎

Copy file name to clipboardExpand all lines: services/database.go
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"encoding/json"
66
"fmt"
7+
"log"
78

89
_ "github.com/lib/pq"
910
"github.com/spf13/viper"
@@ -19,11 +20,11 @@ func Exec(command string) error {
1920
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", yugabyteHost, yugabytePort, yugabyteUser, yugabytePassword, yugabyteDBName)
2021
db, err := sql.Open("postgres", psqlInfo)
2122
if err != nil {
22-
print(err)
23+
log.Fatal(err)
2324
}
2425

2526
if _, err := db.Exec(command); err != nil {
26-
print(err)
27+
log.Fatal(err)
2728

2829
return fmt.Errorf(err.Error())
2930
}

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.