Working cross domain too

This commit is contained in:
bel
2019-10-20 17:13:00 -06:00
parent b1247f8733
commit a0bf41e04e
4 changed files with 95 additions and 33 deletions

View File

@@ -3,9 +3,11 @@ package oauth2
import (
"errors"
"fmt"
"local/oauth2"
"local/oauth2/oauth2client"
"local/oauth2/oauth2server/config"
"local/oauth2/oauth2server/server"
"log"
"net/http"
"net/http/cookiejar"
"net/http/httptest"
@@ -125,33 +127,44 @@ func clientShouldNotRedir(c *http.Client, dummy string) error {
func testAuth(oauth2server, dummy string) error {
c := makeClient()
log.Println("should redir...")
if err := clientShouldRedir(c, dummy); err != nil {
return err
}
if err := clientLogin(c, oauth2server); err != nil {
log.Println("client login...")
access, err := clientLogin(c, oauth2server)
if err != nil {
return err
}
if err := clientShouldNotRedir(c, dummy); err != nil {
log.Println("client should not redir...")
if err := clientShouldNotRedir(c, dummy+"?"+oauth2.COOKIE+"="+access); err != nil {
return err
}
if !strings.Contains(fmt.Sprint(c.Jar), oauth2.COOKIE) {
return errors.New("cookie jar empty:" + fmt.Sprint(c.Jar))
}
return nil
}
func clientLogin(c *http.Client, oauth2server string) error {
req, _ := http.NewRequest("POST", oauth2server+"/authorize", strings.NewReader("username=abc"))
func clientLogin(c *http.Client, oauth2server string) (string, error) {
req, _ := http.NewRequest("POST", oauth2server+"/authorize?"+oauth2.REDIRECT+"="+oauth2server+"/", strings.NewReader("username=abc"))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := c.Do(req)
if err != nil {
return err
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status; %v", resp.StatusCode)
if resp.StatusCode == http.StatusUnauthorized {
return "", fmt.Errorf("bad status; %v", resp.StatusCode)
}
if resp.Request.URL.Path != "/" {
return fmt.Errorf("login response path wrong: %v", resp.Request.URL)
return "", fmt.Errorf("login response path wrong: %v", resp.Request.URL.Path)
}
return nil
a := resp.Request.URL.Query().Get(oauth2.COOKIE)
if a == "" {
return "", fmt.Errorf("login and redir didnt set cookie: %v", a)
}
return a, nil
}
func makeClient() *http.Client {