Skip to content

Commit

Permalink
Merge pull request #1 from tehmoon/workflow
Browse files Browse the repository at this point in the history
workflow and update code
  • Loading branch information
tehmoon authored Dec 29, 2020
2 parents 7bf04f5 + 7c5b0cb commit 77011f8
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Go
on:
pull_request:
types: [synchronize, review_requested, edited, opened]
push:
branches:
- "master"
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '^1.15.6' # The Go version to download (if necessary) and use.
- run: |
set -euxfo pipefail
for dir in auth-oath auth-oathd
do (
cd "${dir}"
set +e; go get -v ./...; set -e
go build .
) done
mkdir -pv artifacts/linux-x86_64
mv auth-oath/auth-oath auth-oathd/auth-oathd artifacts/linux-x86_64
- name: upload
uses: actions/upload-artifact@master
with:
name: artifacts
path: artifacts
2 changes: 1 addition & 1 deletion auth-oath/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func start(username, password, f string) (error) {
}

if ! message.Ok {
return errors.New("Non authorized")
return errors.New("Not authorized")
}

return nil
Expand Down
Binary file removed auth-oathd/auth-oathd
Binary file not shown.
41 changes: 30 additions & 11 deletions auth-oathd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ type User struct {
pin string `json:"-"`
}

func (u *User) Authorize(password string) (bool) {
type OathFlags struct {
Base32 bool
}

func (u *User) Authorize(password string, flags *OathFlags) (bool) {
now := time.Now()

if password == u.pin {
Expand All @@ -33,7 +37,7 @@ func (u *User) Authorize(password string) (bool) {
}
}

ok := authorize(u.Key, password, now)
ok := authorize(u.Key, password, now, flags)
if ! ok {
log.Printf("[-] User %q wrong password", u.Name)
return false
Expand All @@ -47,12 +51,12 @@ func (u *User) Authorize(password string) (bool) {

type Users []*User

func (uu Users) Authorize(message *IngoingMessage) (bool) {
func (uu Users) Authorize(message *IngoingMessage, flags *OathFlags) (bool) {
for _, u := range uu {
if u.Name == message.Name {
log.Printf("[+] Found user %q in database", message.Name)

ok := u.Authorize(message.Password)
ok := u.Authorize(message.Password, flags)
if ok {
log.Printf("[+] Authorizing user %q in database", message.Name)
return true
Expand All @@ -67,15 +71,24 @@ func (uu Users) Authorize(message *IngoingMessage) (bool) {
return false
}

func authorize(key, password string, now time.Time) (bool) {
func authorize(key, password string, now time.Time, flags *OathFlags) (bool) {
if password == "" {
log.Printf("[-] Password is empty")
return false
}

time := now.UTC().Format(time.RFC3339)

command := []string{"oathtool", "--totp=sha512", "-b", "-d", "8", "-N", time, key, password,}
command := []string{"oathtool",}
command = append(command, "--totp=sha512")

// Flags should go to the database so it's user specific
if flags.Base32 {
command = append(command, "-b")
}

command = append(command, []string{"-d", "8", "-N", time, key, password,}...)

cmd := exec.Command(command[0], command[1:]...)

output, err := cmd.CombinedOutput()
Expand All @@ -92,6 +105,7 @@ var (
FlagSocketPath string
FlagSocketUser string
FlagSocketGroup string
FlagBase32 bool
ErrBadFlag error = errors.New("Error bad flag")
)

Expand All @@ -100,6 +114,7 @@ func init() {
flag.StringVar(&FlagSocketPath, "socket", "", "Path to the socket file")
flag.StringVar(&FlagSocketUser, "user", "", "Set user name on the socket file")
flag.StringVar(&FlagSocketGroup, "group", "", "Set group name on the socket file")
flag.BoolVar(&FlagBase32, "base32", false, "Use base32 instead of hex")

flag.Parse()
}
Expand All @@ -119,6 +134,10 @@ func main() {
os.Exit(2)
}

flags := &OathFlags{
Base32: FlagBase32,
}

u, g, err := checkUserGroup(FlagSocketUser, FlagSocketGroup)
if err != nil {
err = errors.Wrap(err, "Error in -user or -group flag")
Expand All @@ -136,7 +155,7 @@ func main() {
os.Exit(2)
}

err = start(users, u, g, FlagSocketPath)
err = start(users, u, g, FlagSocketPath, flags)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())

Expand Down Expand Up @@ -193,7 +212,7 @@ func removeFile(f string) (error) {
return os.Remove(f)
}

func start(users Users, u, g int, f string) (error) {
func start(users Users, u, g int, f string, flags *OathFlags) (error) {
l, err := listenUnix(u, g, f)
if err != nil {
return errors.Wrapf(err, "Error listening on socket %q", f)
Expand All @@ -217,7 +236,7 @@ func start(users Users, u, g int, f string) (error) {

log.Printf("[+] New connection\n")

processAccept(au.UnixConn, users)
processAccept(au.UnixConn, users, flags)

log.Printf("[-] Connection closed\n")
}
Expand All @@ -235,7 +254,7 @@ type OutgoingMessage struct {
Ok bool `json:"ok"`
}

func processAccept(unix *net.UnixConn, users Users) {
func processAccept(unix *net.UnixConn, users Users, flags *OathFlags) {
defer unix.Close()

payload, err := ioutil.ReadAll(unix)
Expand All @@ -252,7 +271,7 @@ func processAccept(unix *net.UnixConn, users Users) {
return
}

ok := users.Authorize(message)
ok := users.Authorize(message, flags)
if ok {
payload, err = json.Marshal(&OutgoingMessage{Ok: true,})
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions auth-oathd/test-users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"user": "blih",
"key": "7a0087bd035cca49c39b7a80b39e4a5953e774bdc90075737132a5f67b640ec4"
}
]

0 comments on commit 77011f8

Please sign in to comment.