Skip to content

Commit cc307d6

Browse files
committed
feat: routes and encryption
1 parent eeac8da commit cc307d6

19 files changed

Lines changed: 1180 additions & 639 deletions

cmd/main.go

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"identitysphere-api/pkg/handlers"
56
"identitysphere-api/services"
67
"identitysphere-api/store"
@@ -9,12 +10,31 @@ import (
910

1011
_ "identitysphere-api/docs" // Swaggo generates docs in this package
1112

13+
"github.com/spf13/viper"
1214
httpSwagger "github.com/swaggo/http-swagger"
1315
)
1416

17+
func getConfig() {
18+
// Set the base name of the config file, without the file extension.
19+
viper.SetConfigName("config")
20+
// Set the path to look for the config file in.
21+
viper.AddConfigPath(".")
22+
// Read in environment variables that match
23+
viper.AutomaticEnv()
24+
// If a config file is found, read it in.
25+
if err := viper.ReadInConfig(); err == nil {
26+
fmt.Println("Using config file:", viper.ConfigFileUsed())
27+
} else {
28+
fmt.Println("Error reading config file:", err)
29+
}
30+
}
31+
1532
func Start() {
33+
getConfig()
34+
dbPath := viper.GetString("service.badger_path")
35+
secret := viper.GetString("service.db_encryption_key")
1636
// Initialize the data store (e.g., database connection)
17-
store, err := store.NewStore()
37+
store, err := store.NewStore(dbPath, secret)
1838
if err != nil {
1939
log.Fatalf("Failed to initialize the database: %v", err)
2040
}
@@ -31,7 +51,13 @@ func Start() {
3151
// if err != nil {
3252
// log.Fatalf("Failed to create policies: %v", err)
3353
// }
54+
apiKey := viper.GetString("api.x-api-key")
55+
fmt.Println("=======================")
56+
fmt.Println("\033[32m", "------x-api-key------", "\033[0m")
57+
fmt.Println("\033[32m", apiKey, "\033[0m")
58+
fmt.Println("=======================")
3459
// Initialize handlers with services
60+
m := handlers.NewMiddlewareService(apiKey)
3561
appHandler := handlers.NewAppHandler(ssiService, store)
3662
authProviderHandler := handlers.NewAuthProviderHandler(ssiService, store)
3763
policyHandler := handlers.NewPolicyHandler(ssiService, store)
@@ -44,26 +70,32 @@ func Start() {
4470
url, //The url pointing to API definition
4571
))
4672
// Set up routes
47-
http.HandleFunc("/applications", handlers.ChainMiddleware(appHandler.HandleApplications, handlers.EnableCORS, handlers.LoggingMiddleware))
48-
http.HandleFunc("/application/", appHandler.GetConfig)
49-
http.HandleFunc("/auth-provider", handlers.EnableCORS(authProviderHandler.GetAuthConnectorHandler))
50-
http.HandleFunc("/auth-provider/link", handlers.EnableCORS(authProviderHandler.LinkAuthProviderHandler))
51-
http.HandleFunc("/auth-provider/unlink", handlers.EnableCORS(authProviderHandler.UnLinkAuthProviderHandler))
52-
http.HandleFunc("/policies", handlers.EnableCORS(policyHandler.GetPolicyHandler))
53-
http.HandleFunc("/create-policy", handlers.EnableCORS(policyHandler.CreatePolicyHandler))
54-
http.HandleFunc("/attach-policy", handlers.EnableCORS(policyHandler.AttachPolicyHandler))
55-
http.HandleFunc("/callback/", callbackHandler.HandleCallback)
56-
http.HandleFunc("/me/", callbackHandler.HandleMe)
73+
// application owner access
74+
http.HandleFunc("/applications", m.ChainMiddleware(m.XApiKeyMiddleware, m.LoggingMiddleware)(appHandler.HandleApplications))
75+
76+
http.HandleFunc("/auth-provider", m.ChainMiddleware(m.XApiKeyMiddleware, m.LoggingMiddleware)(authProviderHandler.GetAuthConnectorHandler))
77+
http.HandleFunc("/auth-provider/link", m.ChainMiddleware(m.XApiKeyMiddleware, m.LoggingMiddleware)(authProviderHandler.LinkAuthProviderHandler))
78+
http.HandleFunc("/auth-provider/unlink", m.ChainMiddleware(m.XApiKeyMiddleware, m.LoggingMiddleware)(authProviderHandler.UnLinkAuthProviderHandler))
79+
80+
http.HandleFunc("/policies", m.ChainMiddleware(m.XApiKeyMiddleware, m.LoggingMiddleware)(policyHandler.GetPolicyHandler))
81+
http.HandleFunc("/create-policy", m.ChainMiddleware(m.XApiKeyMiddleware, m.LoggingMiddleware)(policyHandler.CreatePolicyHandler))
82+
http.HandleFunc("/attach-policy", m.ChainMiddleware(m.XApiKeyMiddleware, m.LoggingMiddleware)(policyHandler.AttachPolicyHandler))
83+
84+
http.HandleFunc("/grant-access", m.ChainMiddleware(m.XApiKeyMiddleware, m.LoggingMiddleware)(authHandler.GrandAccess))
85+
http.HandleFunc("/revoke-access", m.ChainMiddleware(m.XApiKeyMiddleware, m.LoggingMiddleware)(authHandler.RevokeAccess))
86+
http.HandleFunc("/revoke-credential", m.ChainMiddleware(m.XApiKeyMiddleware, m.LoggingMiddleware)(credentialHandler.RevokeOAuthCredential))
5787

58-
http.HandleFunc("/issue-credential", credentialHandler.IssueOAuthCredential)
59-
http.HandleFunc("/revoke-credential", credentialHandler.RevokeOAuthCredential)
88+
// application itself access
89+
http.HandleFunc("/validate-access", m.ChainMiddleware(m.LoggingMiddleware)(authHandler.VerifyAccess))
90+
http.HandleFunc("/issue-credential", m.ChainMiddleware(m.EnableCORS, m.LoggingMiddleware)(credentialHandler.IssueOAuthCredential))
91+
// application user access
92+
http.HandleFunc("/callback/", m.ChainMiddleware(m.EnableCORS, m.LoggingMiddleware)(callbackHandler.HandleCallback))
93+
http.HandleFunc("/me/", m.ChainMiddleware(m.EnableCORS, m.LoggingMiddleware)(callbackHandler.HandleMe))
94+
http.HandleFunc("/signup", m.ChainMiddleware(m.EnableCORS, m.LoggingMiddleware)(authHandler.SignUpHandler))
6095

61-
http.HandleFunc("/signup", authHandler.SignUpHandler)
62-
http.HandleFunc("/signin", authHandler.SignInHandler)
96+
http.HandleFunc("/get-access-token", m.ChainMiddleware(m.LoggingMiddleware)(authHandler.GetAccessToken))
97+
http.HandleFunc("/request-access", m.ChainMiddleware(m.LoggingMiddleware)(authHandler.RequestAccess))
6398

64-
http.HandleFunc("/validate-access", authHandler.VerifyAccess)
65-
http.HandleFunc("/grant-access", authHandler.GrandAccess)
66-
http.HandleFunc("/revoke-access", authHandler.RevokeAccess)
6799
// static web page for access_token
68100
fs := http.FileServer(http.Dir("web"))
69101
http.Handle("/web/", http.StripPrefix("/web/", fs))

config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# config.yaml
2+
service:
3+
badger_path: "./badger_db"
4+
db_encryption_key: "badger"
5+
jwt_encryption_key: "random"
6+
api:
7+
x-api-key: "9dfccbf4-8c41-412f-841f-8cc10e4a26be"
8+

0 commit comments

Comments
 (0)