Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
38
Task/HTTPS-Authenticated/Go/https-authenticated-1.go
Normal file
38
Task/HTTPS-Authenticated/Go/https-authenticated-1.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const userPass = "rosetta:code"
|
||||
const unauth = http.StatusUnauthorized
|
||||
|
||||
func hw(w http.ResponseWriter, req *http.Request) {
|
||||
auth := req.Header.Get("Authorization")
|
||||
if !strings.HasPrefix(auth, "Basic ") {
|
||||
log.Print("Invalid authorization:", auth)
|
||||
http.Error(w, http.StatusText(unauth), unauth)
|
||||
return
|
||||
}
|
||||
up, err := base64.StdEncoding.DecodeString(auth[6:])
|
||||
if err != nil {
|
||||
log.Print("authorization decode error:", err)
|
||||
http.Error(w, http.StatusText(unauth), unauth)
|
||||
return
|
||||
}
|
||||
if string(up) != userPass {
|
||||
log.Print("invalid username:password:", string(up))
|
||||
http.Error(w, http.StatusText(unauth), unauth)
|
||||
return
|
||||
}
|
||||
io.WriteString(w, "Goodbye, World!")
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", hw)
|
||||
log.Fatal(http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil))
|
||||
}
|
||||
49
Task/HTTPS-Authenticated/Go/https-authenticated-2.go
Normal file
49
Task/HTTPS-Authenticated/Go/https-authenticated-2.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
userid = "rosetta"
|
||||
password = "code"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Use custom certificate for testing. Not exactly required by task.
|
||||
b, err := ioutil.ReadFile("cert.pem")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
pool := x509.NewCertPool()
|
||||
if ok := pool.AppendCertsFromPEM(b); !ok {
|
||||
log.Fatal("Failed to append cert")
|
||||
}
|
||||
tc := &tls.Config{RootCAs: pool}
|
||||
tr := &http.Transport{TLSClientConfig: tc}
|
||||
client := &http.Client{Transport: tr}
|
||||
req, err := http.NewRequest("GET", "https://127.0.0.1:8080", nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// This one line implements the authentication required for the task.
|
||||
req.SetBasicAuth(userid, password)
|
||||
|
||||
// Make request and show output.
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
b, err = ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue