This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start setting up to merge 2 projects
- Loading branch information
Showing
11 changed files
with
244 additions
and
246 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,65 @@ | ||
package pufferd | ||
|
||
import ( | ||
"bytes" | ||
"crypto/ecdsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"github.com/pufferpanel/apufferi/v4" | ||
"github.com/spf13/viper" | ||
"golang.org/x/crypto/ssh" | ||
"io" | ||
"os" | ||
"sync" | ||
) | ||
|
||
type SFTPAuthorization interface { | ||
Validate(username, password string) (perms *ssh.Permissions, err error) | ||
} | ||
|
||
var publicKey *ecdsa.PublicKey | ||
|
||
var atLocker = &sync.RWMutex{} | ||
|
||
func SetPublicKey(key *ecdsa.PublicKey) { | ||
atLocker.Lock() | ||
defer atLocker.Unlock() | ||
publicKey = key | ||
} | ||
|
||
func GetPublicKey() *ecdsa.PublicKey { | ||
atLocker.RLock() | ||
defer atLocker.RUnlock() | ||
return publicKey | ||
} | ||
|
||
func LoadPublicKey() (*ecdsa.PublicKey, error) { | ||
publicKey := GetPublicKey() | ||
if publicKey != nil { | ||
return publicKey, nil | ||
} | ||
|
||
f, err := os.OpenFile(viper.GetString("auth.publicKey"), os.O_RDONLY, 660) | ||
defer apufferi.Close(f) | ||
|
||
var buf bytes.Buffer | ||
|
||
_, _ = io.Copy(&buf, f) | ||
|
||
block, _ := pem.Decode(buf.Bytes()) | ||
if block == nil { | ||
return nil, ErrKeyNotPEM | ||
} | ||
pub, err := x509.ParsePKIXPublicKey(block.Bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
publicKey, ok := pub.(*ecdsa.PublicKey) | ||
if !ok { | ||
return nil, ErrKeyNotECDSA | ||
} | ||
|
||
SetPublicKey(publicKey) | ||
return publicKey, 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
Oops, something went wrong.