Skip to content

Commit

Permalink
feat: Local authentication is configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
zhaojh329 committed Feb 28, 2021
1 parent 2a08cb5 commit b5ecca7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Config struct {
Token string
WhiteList map[string]bool
DB string
LocalAuth bool
}

func getConfigOpt(yamlCfg *yaml.File, name string, opt interface{}) {
Expand Down Expand Up @@ -51,6 +52,7 @@ func Parse(c *cli.Context) *Config {
SslCacert: c.String("ssl-cacert"),
Token: c.String("token"),
DB: c.String("db"),
LocalAuth: c.Bool("local-auth"),
}

cfg.WhiteList = make(map[string]bool)
Expand Down
16 changes: 9 additions & 7 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ func authorizedDev(devid string, cfg *config.Config) bool {
return ok
}

func httpAuth(c *gin.Context) bool {
addr, _ := net.ResolveTCPAddr("tcp", c.Request.RemoteAddr)
if addr.IP.IsLoopback() {
return true
func httpAuth(cfg *config.Config, c *gin.Context) bool {
if !cfg.LocalAuth {
addr, _ := net.ResolveTCPAddr("tcp", c.Request.RemoteAddr)
if addr.IP.IsLoopback() {
return true
}
}

cookie, err := c.Cookie("sid")
Expand Down Expand Up @@ -98,7 +100,7 @@ func httpStart(br *broker) {
return
}

if !httpAuth(c) {
if !httpAuth(cfg, c) {
c.AbortWithStatus(http.StatusUnauthorized)
}
})
Expand Down Expand Up @@ -193,7 +195,7 @@ func httpStart(br *broker) {
})

r.GET("/authorized/:devid", func(c *gin.Context) {
authorized := authorizedDev(c.Param("devid"), cfg) || httpAuth(c)
authorized := authorizedDev(c.Param("devid"), cfg) || httpAuth(cfg, c)
c.JSON(http.StatusOK, gin.H{
"authorized": authorized,
})
Expand Down Expand Up @@ -225,7 +227,7 @@ func httpStart(br *broker) {
})

r.GET("/alive", func(c *gin.Context) {
if !httpAuth(c) {
if !httpAuth(cfg, c) {
c.AbortWithStatus(http.StatusUnauthorized)
} else {
c.Status(http.StatusOK)
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ func main() {
Value: "rttys.db",
Usage: "sqlite3 database path",
},
&cli.BoolFlag{
Name: "local-auth",
Usage: "need auth for local",
},
},
Action: func(c *cli.Context) error {
runRttys(c)
Expand Down

0 comments on commit b5ecca7

Please sign in to comment.