-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OAuth working. Config refactor to actually work.
- Loading branch information
Showing
735 changed files
with
101,971 additions
and
108,258 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,17 @@ | ||
package apiv1 | ||
|
||
import ( | ||
"github.com/Buhrietoe/brood/server/middleware" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// func apiV1 defines routes of the v1 rest api | ||
func APIV1(api gin.RouterGroup) { | ||
api.Use(middleware.ServerHeader("brood/api/v1")) | ||
|
||
// Test endpoint | ||
api.GET("/ping", func(c *gin.Context) { | ||
c.String(200, "pong") | ||
}) | ||
} |
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,15 @@ | ||
package auth | ||
|
||
import "github.com/Buhrietoe/brood/server/apiv1/auth/oauth" | ||
|
||
type Auth struct { | ||
OAuth oauth.OAuthProvider // OAuth config | ||
} | ||
|
||
// Provider interface for authentication providers | ||
type Provider interface { | ||
Authenticate(username, password string) (authenticated bool, token string, err error) | ||
SetSigningKey(key string) | ||
Connect() error | ||
Close() | ||
} |
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,88 @@ | ||
package oauth | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
|
||
tk "github.com/Buhrietoe/brood/server/apiv1/auth/token" | ||
) | ||
|
||
type OAuthProvider struct { | ||
Host string `toml:"host"` | ||
ClientID string `toml:"client_id"` | ||
ResponseType string `toml:"response_type"` | ||
signingKey string | ||
} | ||
|
||
func (op *OAuthProvider) SetSigningKey(key string) { | ||
op.signingKey = key | ||
} | ||
|
||
func (op *OAuthProvider) Authenticate(username, password string) (authenticated bool, token string, err error) { | ||
tr := &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
} | ||
client := &http.Client{ | ||
Transport: tr, | ||
CheckRedirect: func(req *http.Request, via []*http.Request) error { | ||
return http.ErrUseLastResponse | ||
}, | ||
} | ||
|
||
url := fmt.Sprintf("https://%s/oauth/authorize", op.Host) | ||
|
||
body := bytes.NewBuffer([]byte(`{}`)) | ||
req, err := http.NewRequest("POST", url, body) | ||
|
||
if err != nil { | ||
OAuthFailed(err) | ||
return | ||
} | ||
|
||
q := req.URL.Query() | ||
q.Add("client_id", op.ClientID) | ||
q.Add("response_type", op.ResponseType) | ||
req.URL.RawQuery = q.Encode() | ||
|
||
req.Header.Add("Content-Type", "application/json; charset=utf-8") | ||
req.SetBasicAuth(username, password) | ||
|
||
err = req.ParseForm() | ||
if err != nil { | ||
OAuthFailed(err) | ||
return | ||
} | ||
|
||
resp, err := client.Do(req) | ||
|
||
if err != nil { | ||
OAuthFailed(err) | ||
return | ||
} | ||
|
||
if resp.StatusCode == 302 { | ||
authenticated = true | ||
token = tk.CreateExpiringToken(username, op.signingKey, time.Hour*48, "oauth") | ||
return | ||
} else { | ||
message, _ := ioutil.ReadAll(resp.Body) | ||
OAuthFailed(errors.New(fmt.Sprintf("response code: %d message: %s", resp.StatusCode, message))) | ||
return | ||
} | ||
} | ||
|
||
func OAuthFailed(err error) { | ||
fmt.Println("oauth failed: ", err.Error()) | ||
} | ||
|
||
func (op *OAuthProvider) Connect() error { | ||
return nil | ||
} | ||
func (op *OAuthProvider) Close() { | ||
|
||
} |
Oops, something went wrong.