This repository has been archived by the owner on Feb 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable Google OAuth and move API key to server-side
Masayoshi Mizutani
committed
Feb 10, 2020
1 parent
32ebedb
commit f55297c
Showing
8 changed files
with
248 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type authzService struct { | ||
AllowTable map[string][]string `json:"allow"` | ||
} | ||
|
||
func newAuthzService(filePath string) (*authzService, error) { | ||
raw, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Fail to load authz file: %s", filePath) | ||
} | ||
|
||
srv := authzService{ | ||
AllowTable: make(map[string][]string), | ||
} | ||
if err := json.Unmarshal(raw, &srv); err != nil { | ||
return nil, errors.Wrapf(err, "Fail to parse authz file: %s", filePath) | ||
} | ||
|
||
logger.WithField("authz", srv.AllowTable).Info("Read authorization table") | ||
return &srv, nil | ||
} | ||
|
||
func (x *authzService) allowedTags(user string) ([]string, error) { | ||
allowed, ok := x.AllowTable[user] | ||
if !ok { | ||
return nil, fmt.Errorf("Not permitted") | ||
} | ||
|
||
return allowed, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var logger = logrus.New() | ||
|
||
var logLevelMap = map[string]logrus.Level{ | ||
"trace": logrus.TraceLevel, | ||
"debug": logrus.DebugLevel, | ||
"info": logrus.InfoLevel, | ||
"warn": logrus.WarnLevel, | ||
"error": logrus.ErrorLevel, | ||
} | ||
|
||
func setupLogger(logLevel string) error { | ||
level, ok := logLevelMap[logLevel] | ||
if !ok { | ||
return fmt.Errorf("Invalid log level: %s", logLevel) | ||
} | ||
logger.SetLevel(level) | ||
logger.SetFormatter(&logrus.JSONFormatter{}) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gin-contrib/sessions" | ||
"github.com/gin-contrib/sessions/cookie" | ||
"github.com/gin-contrib/static" | ||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type arguments struct { | ||
LogLevel string | ||
Endpoint string | ||
BindAddress string | ||
BindPort int | ||
StaticContents string | ||
HelloReply string | ||
APIKey string | ||
SecretArn string | ||
AuthzFilePath string | ||
|
||
// Google OAuth options | ||
GoogleOAuthConfig string | ||
|
||
// JWT | ||
JWTSecret string | ||
} | ||
|
||
func runServer(args arguments) error { | ||
if err := setupLogger(args.LogLevel); err != nil { | ||
return err | ||
} | ||
|
||
logger.WithFields(logrus.Fields{ | ||
"args": args, | ||
}).Info("Given options") | ||
|
||
r := gin.Default() | ||
store := cookie.NewStore([]byte("auth")) | ||
r.Use(sessions.Sessions("strix", store)) | ||
r.Use(static.Serve("/", static.LocalFile(args.StaticContents, false))) | ||
|
||
r.GET("/hello/revision", func(c *gin.Context) { | ||
c.String(200, args.HelloReply) | ||
}) | ||
|
||
// Setup session manager | ||
authz, err := newAuthzService(args.AuthzFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ssnMgr := newSessionManager(args.JWTSecret) | ||
authCheck := func(c *gin.Context) { | ||
user, err := ssnMgr.validate(c) | ||
if err != nil { | ||
logger.WithError(err).Warn("Authentication Fail") | ||
c.JSON(http.StatusUnauthorized, gin.H{"msg": "Authentication failed"}) | ||
} else { | ||
c.Set("user", user.UserID) | ||
c.Next() | ||
} | ||
} | ||
|
||
// Auth route group | ||
authGroup := r.Group("/auth") | ||
if err := setupAuth(ssnMgr, authGroup); err != nil { | ||
return err | ||
} | ||
if args.GoogleOAuthConfig != "" { | ||
if err := setupAuthGoogle(ssnMgr, args.GoogleOAuthConfig, authGroup); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// API route group | ||
apiGroup := r.Group("/api/v1") | ||
apiGroup.Use(authCheck) | ||
if err := setupAPI(authz, args.APIKey, args.Endpoint, apiGroup); err != nil { | ||
return err | ||
} | ||
|
||
// Start server | ||
if err := r.Run(fmt.Sprintf("%s:%d", args.BindAddress, args.BindPort)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters