-
Notifications
You must be signed in to change notification settings - Fork 2
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
14 changed files
with
686 additions
and
69 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
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,102 @@ | ||
package login | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/ecdh" | ||
"crypto/rand" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/rotisserie/eris" | ||
) | ||
|
||
const ( | ||
decryptionErrorMsg = "cannot decrypt access token" | ||
) | ||
|
||
type Encryption struct { | ||
curve ecdh.Curve | ||
privateKey *ecdh.PrivateKey | ||
publicKey *ecdh.PublicKey | ||
} | ||
|
||
type AccessTokenResponse struct { | ||
AccessToken string `json:"access_token"` | ||
PublicKey string `json:"pub_key"` | ||
Nonce string `json:"nonce"` | ||
} | ||
|
||
// NewEncryption creates a new Encryption struct | ||
func NewEncryption() (Encryption, error) { | ||
enc := Encryption{} | ||
err := enc.generateKeys() | ||
if err != nil { | ||
return enc, err | ||
} | ||
return enc, nil | ||
} | ||
|
||
// generateKeys generates a private and public key pair | ||
func (enc *Encryption) generateKeys() error { | ||
enc.curve = ecdh.P256() | ||
privateKey, err := enc.curve.GenerateKey(rand.Reader) | ||
if err != nil { | ||
return eris.Wrap(err, "cannot generate keys") | ||
} | ||
enc.privateKey = privateKey | ||
enc.publicKey = privateKey.PublicKey() | ||
return nil | ||
} | ||
|
||
// encodedPublicKey returns the public key as a hex string | ||
func (enc Encryption) EncodedPublicKey() string { | ||
return hex.EncodeToString(enc.publicKey.Bytes()) | ||
} | ||
|
||
// decryptAccessToken decrypts the access token using the private key and nonce | ||
func (enc Encryption) DecryptAccessToken(accessToken string, publicKey string, nonce string) (string, error) { | ||
decodedAccessToken, err := hex.DecodeString(accessToken) | ||
if err != nil { | ||
return "", eris.Wrap(err, decryptionErrorMsg) | ||
} | ||
|
||
decodedNonce, err := hex.DecodeString(nonce) | ||
if err != nil { | ||
return "", eris.Wrap(err, decryptionErrorMsg) | ||
} | ||
|
||
decodedPublicKey, err := hex.DecodeString(publicKey) | ||
if err != nil { | ||
return "", eris.Wrap(err, decryptionErrorMsg) | ||
} | ||
|
||
fmt.Println("Decoded Pub Key : ", string(decodedPublicKey)) | ||
|
||
remotePublicKey, err := enc.curve.NewPublicKey(decodedPublicKey) | ||
if err != nil { | ||
return "", eris.Wrap(err, decryptionErrorMsg) | ||
} | ||
|
||
secret, err := enc.privateKey.ECDH(remotePublicKey) | ||
if err != nil { | ||
return "", eris.Wrap(err, decryptionErrorMsg) | ||
} | ||
|
||
block, err := aes.NewCipher(secret) | ||
if err != nil { | ||
return "", eris.Wrap(err, decryptionErrorMsg) | ||
} | ||
|
||
aesgcm, err := cipher.NewGCM(block) | ||
if err != nil { | ||
return "", eris.Wrap(err, decryptionErrorMsg) | ||
} | ||
|
||
decryptedAccessToken, err := aesgcm.Open(nil, decodedNonce, decodedAccessToken, nil) | ||
if err != nil { | ||
return "", eris.Wrap(err, decryptionErrorMsg) | ||
} | ||
|
||
return string(decryptedAccessToken), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//go:build darwin | ||
|
||
package login | ||
|
||
import ( | ||
"context" | ||
"os/exec" | ||
) | ||
|
||
func RunOpenCmd(ctx context.Context, input string) error { | ||
cmd := exec.CommandContext(ctx, "open", input) | ||
return cmd.Run() | ||
} |
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,17 @@ | ||
//go:build !windows && !darwin | ||
|
||
package login | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func RunOpenCmd(ctx context.Context, input string) error { | ||
if f, err := os.ReadFile("/proc/sys/kernel/osrelease"); err == nil && bytes.Contains(f, []byte("WSL")) { | ||
return exec.CommandContext(ctx, "wslview", input).Run() | ||
} | ||
return exec.CommandContext(ctx, "xdg-open", input).Run() | ||
} |
Oops, something went wrong.