Skip to content

Commit

Permalink
[feature] authentication middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Mcklmo committed Nov 28, 2023
1 parent d1dac92 commit 8a010df
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ func main() {
config.Load("..", &appRepository)
virtuosoRepository := virtuoso.NewVirtuosoRepository(appRepository.VirtuosoURL, appRepository.VirtuosoUsername, appRepository.VirtuosoPassword)
service := graph.NewService(virtuosoRepository)
router := rest.NewRouter(service, graph.OntologyGraphURI(appRepository.OntologyGraphURI), graph.KnowledgeBaseGraphURI(appRepository.GraphURI))
router := rest.NewRouter(service, graph.OntologyGraphURI(appRepository.OntologyGraphURI), graph.KnowledgeBaseGraphURI(appRepository.GraphURI), appRepository.APISecret)
router.Run(":8000")
}
2 changes: 2 additions & 0 deletions pkg/config/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Config struct {
OntologyGraphURI GraphURI
GraphURI GraphURI
TestGraphURI GraphURI
APISecret string
VirtuosoUsername string
VirtuosoPassword string
}
Expand All @@ -36,6 +37,7 @@ func Load(rootPath string, config *Config) {
config.VirtuosoUsername = mustGetENV("VIRTUOSO_USERNAME")
config.VirtuosoPassword = mustGetENV("VIRTUOSO_PASSWORD")
config.OntologyGraphURI = GraphURI(mustGetENV("VIRTUOSO_ONTOLOGY_GRAPH_URI"))
config.APISecret = mustGetENV("API_SECRET")
}

func mustGetENV(key string) string {
Expand Down
11 changes: 11 additions & 0 deletions pkg/http/rest/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ func validateGraphParameter(validGraphs []graph.TargetGraph) gin.HandlerFunc {
c.Abort()
}
}

func authenticate(secret string) gin.HandlerFunc {
return func(c *gin.Context) {
if c.GetHeader("Authorization") != secret {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
c.Abort()
return
}
c.Next()
}
}
3 changes: 2 additions & 1 deletion pkg/http/rest/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ const (
POST Method = http.MethodPost
)

func NewRouter(s graph.Service, ontologyGraph graph.OntologyGraphURI, knowledgeBaseGraph graph.KnowledgeBaseGraphURI) *gin.Engine {
func NewRouter(s graph.Service, ontologyGraph graph.OntologyGraphURI, knowledgeBaseGraph graph.KnowledgeBaseGraphURI, apiSecret string) *gin.Engine {
router := gin.Default()
router.Use(validateGraphParameter([]graph.TargetGraph{graph.TargetGraph(ontologyGraph), graph.TargetGraph(knowledgeBaseGraph)}))
router.Use(authenticate(apiSecret))
router.GET(string(Triples), getHandler(s))
router.POST(string(Triples), postHandler(s))

Expand Down

0 comments on commit 8a010df

Please sign in to comment.