-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AWS IAM Identity Center device code flow automation (#765)
undefined
- Loading branch information
Showing
13 changed files
with
500 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Package chromemsg implements the Chrome native messaging protocol: | ||
// https://developer.chrome.com/docs/extensions/develop/concepts/native-messaging | ||
package chromemsg | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
) | ||
|
||
type Server struct { | ||
Input io.Reader | ||
Output io.Writer | ||
} | ||
|
||
func (s *Server) Read(p []byte) (int, error) { | ||
// From the Chrome developer docs: | ||
// "Each message is serialized using JSON, UTF-8 encoded and is preceded with 32-bit message length in native byte order." | ||
// we need to read the 32-bit message length value first. | ||
var b [4]byte | ||
_, err := io.ReadFull(s.Input, b[:]) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
size := binary.LittleEndian.Uint32(b[:]) | ||
r := io.LimitReader(s.Input, int64(size)) | ||
|
||
return r.Read(p) | ||
} | ||
|
||
func (s *Server) Write(p []byte) (n int, err error) { | ||
// From the Chrome developer docs: | ||
// "Each message is serialized using JSON, UTF-8 encoded and is preceded with 32-bit message length in native byte order." | ||
// we need to read the 32-bit message length value first. | ||
|
||
header := []byte{0, 0, 0, 0} // 32-bit | ||
binary.LittleEndian.PutUint32(header, uint32(len(p))) | ||
|
||
n, err = s.Output.Write(header) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
n2, err := s.Output.Write(p) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return n + n2, 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,34 @@ | ||
// Package chromemsg implements the Chrome native messaging protocol: | ||
// https://developer.chrome.com/docs/extensions/develop/concepts/native-messaging | ||
package chromemsg | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
) | ||
|
||
// TestServer_ReadWrite tests the protocol by creating a server where the output | ||
// is connected to the input. We write to the output, and then try and read the | ||
// same message back from the input. | ||
func TestServer_ReadWrite(t *testing.T) { | ||
r, w := io.Pipe() | ||
|
||
server := &Server{Input: r, Output: w} | ||
|
||
message := []byte("Hello, Chrome!") | ||
|
||
go func() { | ||
if _, err := server.Write(message); err != nil { | ||
t.Errorf("failed to write message: %v", err) | ||
} | ||
}() | ||
|
||
readBuffer := make([]byte, len(message)) | ||
if _, err := server.Read(readBuffer); err != nil { | ||
t.Errorf("failed to read message: %v", err) | ||
} | ||
|
||
if string(readBuffer) != string(message) { | ||
t.Errorf("expected %s, got %s", message, readBuffer) | ||
} | ||
} |
Oops, something went wrong.