forked from asoul-sig/asouldocs
-
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.
- Loading branch information
Showing
8 changed files
with
193 additions
and
19 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
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,72 @@ | ||
// Copyright 2015 Unknwon | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"): you may | ||
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package models | ||
|
||
import ( | ||
"fmt" | ||
"path" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/Unknwon/com" | ||
"gopkg.in/ini.v1" | ||
) | ||
|
||
type protector struct { | ||
lock sync.Mutex | ||
HasProtection bool | ||
Users map[string]string | ||
Resources map[string]map[string]bool | ||
} | ||
|
||
var ( | ||
Protector = &protector{ | ||
Users: make(map[string]string), | ||
Resources: make(map[string]map[string]bool), | ||
} | ||
) | ||
|
||
func reloadProtects(localRoot string) error { | ||
Protector.lock.Lock() | ||
defer Protector.lock.Unlock() | ||
|
||
protectPath := path.Join(localRoot, "protect.ini") | ||
if !com.IsFile(protectPath) { | ||
return nil | ||
} | ||
|
||
Protector.HasProtection = true | ||
|
||
cfgs, err := ini.Load(protectPath) | ||
if err != nil { | ||
return fmt.Errorf("Fail to load protect.ini: %v", err) | ||
} | ||
|
||
for _, k := range cfgs.Section("user").Keys() { | ||
Protector.Users[k.Name()] = strings.ToLower(k.Value()) | ||
} | ||
|
||
fmt.Println("\nProtected Resources:") | ||
for _, k := range cfgs.Section("auth").Keys() { | ||
fmt.Println("➜ ", k.Name()) | ||
Protector.Resources[k.Name()] = make(map[string]bool) | ||
for _, name := range k.Strings(",") { | ||
fmt.Println(" ✓ ", name) | ||
Protector.Resources[k.Name()][name] = true | ||
} | ||
} | ||
|
||
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
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,89 @@ | ||
// Copyright 2015 Unknwon | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"): you may | ||
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package routers | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"strings" | ||
|
||
"github.com/peachdocs/peach/models" | ||
"github.com/peachdocs/peach/modules/middleware" | ||
) | ||
|
||
func authRequired(ctx *middleware.Context) { | ||
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=\".\"") | ||
ctx.Error(401) | ||
} | ||
|
||
func basicAuthDecode(encoded string) (string, string, error) { | ||
s, err := base64.StdEncoding.DecodeString(encoded) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
auth := strings.SplitN(string(s), ":", 2) | ||
return auth[0], auth[1], nil | ||
} | ||
|
||
func encodeMd5(str string) string { | ||
m := md5.New() | ||
m.Write([]byte(str)) | ||
return hex.EncodeToString(m.Sum(nil)) | ||
} | ||
|
||
func Protect(ctx *middleware.Context) { | ||
if !models.Protector.HasProtection { | ||
return | ||
} | ||
|
||
// Check if resource is protected. | ||
allows, yes := models.Protector.Resources[strings.TrimPrefix(ctx.Req.URL.Path, "/docs/")] | ||
if !yes { | ||
return | ||
} | ||
|
||
// Check if auth is presented. | ||
authHead := ctx.Req.Header.Get("Authorization") | ||
if len(authHead) == 0 { | ||
authRequired(ctx) | ||
return | ||
} | ||
|
||
auths := strings.Fields(authHead) | ||
if len(auths) != 2 || auths[0] != "Basic" { | ||
ctx.Error(401) | ||
return | ||
} | ||
|
||
uname, passwd, err := basicAuthDecode(auths[1]) | ||
if err != nil { | ||
ctx.Error(401) | ||
return | ||
} | ||
|
||
// Check if auth is valid. | ||
if models.Protector.Users[uname] != encodeMd5(passwd) { | ||
ctx.Error(401) | ||
return | ||
} | ||
|
||
// Check if user has access to the resource. | ||
if !allows[uname] { | ||
ctx.Error(403) | ||
return | ||
} | ||
} |