forked from Place1/wg-access-server
-
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.
wip wip wip wip
- Loading branch information
Showing
44 changed files
with
2,515 additions
and
11,007 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
config.yaml |
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,13 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | ||
|
||
OUT_DIR="$DIR/proto/proto" | ||
|
||
mkdir -p "$OUT_DIR" || true | ||
|
||
protoc \ | ||
-I proto/ \ | ||
proto/*.proto \ | ||
--go_out="plugins=grpc:$OUT_DIR" |
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,25 @@ | ||
package auth | ||
|
||
type AuthConfig struct { | ||
OIDC *OIDCConfig `yaml:"oidc"` | ||
Gitlab *GitlabConfig `yaml:"gitlab"` | ||
Basic *BasicAuthConfig `yaml:"basic"` | ||
} | ||
|
||
func (c *AuthConfig) Providers() []*Provider { | ||
providers := []*Provider{} | ||
|
||
if c.OIDC != nil { | ||
providers = append(providers, c.OIDC.Provider()) | ||
} | ||
|
||
if c.Gitlab != nil { | ||
providers = append(providers, c.Gitlab.Provider()) | ||
} | ||
|
||
if c.Basic != nil { | ||
providers = append(providers, c.Basic.Provider()) | ||
} | ||
|
||
return providers | ||
} |
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,85 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/tg123/go-htpasswd" | ||
) | ||
|
||
type BasicAuthConfig struct { | ||
// Users is a list of htpasswd encoded username:password pairs | ||
// supports BCrypt, Sha, Ssha, Md5 | ||
// example: "htpasswd -nB <username>" | ||
// copy the result into your user's array | ||
Users []string `yaml:"users"` | ||
} | ||
|
||
func (c *BasicAuthConfig) Provider() *Provider { | ||
return &Provider{ | ||
RegisterRoutes: func(router *mux.Router, runtime *ProviderRuntime) error { | ||
router.HandleFunc("/login", basicAuthLogin(c, runtime)) | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func basicAuthLogin(c *BasicAuthConfig, runtime *ProviderRuntime) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
|
||
u, p, ok := r.BasicAuth() | ||
if !ok { | ||
w.Header().Set("WWW-Authenticate", `Basic realm="site"`) | ||
w.WriteHeader(http.StatusUnauthorized) | ||
fmt.Fprintln(w, "unauthorized") | ||
return | ||
} | ||
|
||
if ok := checkCreds(c.Users, u, p); ok { | ||
runtime.SetSession(w, r, &AuthSession{ | ||
Identity: &Identity{ | ||
Subject: u, | ||
}, | ||
}) | ||
} | ||
|
||
runtime.Done(w, r) | ||
} | ||
} | ||
|
||
func checkCreds(users []string, username string, password string) bool { | ||
for _, user := range users { | ||
if u, p, ok := parsehtpassword(user); ok { | ||
if u == username { | ||
return checkhtpasswd(p, password) | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func parsehtpassword(user string) (string, string, bool) { | ||
segments := strings.SplitN(user, ":", 2) | ||
if len(segments) >= 1 { | ||
return segments[0], segments[1], true | ||
} | ||
return "", "", false | ||
} | ||
|
||
func checkhtpasswd(required string, given string) bool { | ||
if encoded, err := htpasswd.AcceptBcrypt(required); encoded != nil && err == nil { | ||
return encoded.MatchesPassword(given) | ||
} | ||
if encoded, err := htpasswd.AcceptSha(required); encoded != nil && err == nil { | ||
return encoded.MatchesPassword(given) | ||
} | ||
if encoded, err := htpasswd.AcceptSsha(required); encoded != nil && err == nil { | ||
return encoded.MatchesPassword(given) | ||
} | ||
if encoded, err := htpasswd.AcceptMd5(required); encoded != nil && err == nil { | ||
return encoded.MatchesPassword(given) | ||
} | ||
return false | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.