replace zxq.co/ripple/hanayo
This commit is contained in:
411
vendor/github.com/RangelReale/osin/example/complete/complete.go
generated
vendored
Normal file
411
vendor/github.com/RangelReale/osin/example/complete/complete.go
generated
vendored
Normal file
@@ -0,0 +1,411 @@
|
||||
package main
|
||||
|
||||
// Open url in browser:
|
||||
// http://localhost:14000/app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/RangelReale/osin"
|
||||
"github.com/RangelReale/osin/example"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func main() {
|
||||
sconfig := osin.NewServerConfig()
|
||||
sconfig.AllowedAuthorizeTypes = osin.AllowedAuthorizeType{osin.CODE, osin.TOKEN}
|
||||
sconfig.AllowedAccessTypes = osin.AllowedAccessType{osin.AUTHORIZATION_CODE,
|
||||
osin.REFRESH_TOKEN, osin.PASSWORD, osin.CLIENT_CREDENTIALS, osin.ASSERTION}
|
||||
sconfig.AllowGetAccessRequest = true
|
||||
sconfig.AllowClientSecretInParams = true
|
||||
server := osin.NewServer(sconfig, example.NewTestStorage())
|
||||
|
||||
// Authorization code endpoint
|
||||
http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ar := server.HandleAuthorizeRequest(resp, r); ar != nil {
|
||||
if !example.HandleLoginPage(ar, w, r) {
|
||||
return
|
||||
}
|
||||
ar.UserData = struct{ Login string }{Login: "test"}
|
||||
ar.Authorized = true
|
||||
server.FinishAuthorizeRequest(resp, r, ar)
|
||||
}
|
||||
if resp.IsError && resp.InternalError != nil {
|
||||
fmt.Printf("ERROR: %s\n", resp.InternalError)
|
||||
}
|
||||
if !resp.IsError {
|
||||
resp.Output["custom_parameter"] = 187723
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// Access token endpoint
|
||||
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ar := server.HandleAccessRequest(resp, r); ar != nil {
|
||||
switch ar.Type {
|
||||
case osin.AUTHORIZATION_CODE:
|
||||
ar.Authorized = true
|
||||
case osin.REFRESH_TOKEN:
|
||||
ar.Authorized = true
|
||||
case osin.PASSWORD:
|
||||
if ar.Username == "test" && ar.Password == "test" {
|
||||
ar.Authorized = true
|
||||
}
|
||||
case osin.CLIENT_CREDENTIALS:
|
||||
ar.Authorized = true
|
||||
case osin.ASSERTION:
|
||||
if ar.AssertionType == "urn:osin.example.complete" && ar.Assertion == "osin.data" {
|
||||
ar.Authorized = true
|
||||
}
|
||||
}
|
||||
server.FinishAccessRequest(resp, r, ar)
|
||||
}
|
||||
if resp.IsError && resp.InternalError != nil {
|
||||
fmt.Printf("ERROR: %s\n", resp.InternalError)
|
||||
}
|
||||
if !resp.IsError {
|
||||
resp.Output["custom_parameter"] = 19923
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// Information endpoint
|
||||
http.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ir := server.HandleInfoRequest(resp, r); ir != nil {
|
||||
server.FinishInfoRequest(resp, r, ir)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// Application home endpoint
|
||||
http.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("<html><body>"))
|
||||
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"/authorize?response_type=code&client_id=1234&state=xyz&scope=everything&redirect_uri=%s\">Code</a><br/>", url.QueryEscape("http://localhost:14000/appauth/code"))))
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"/authorize?response_type=token&client_id=1234&state=xyz&scope=everything&redirect_uri=%s\">Implicit</a><br/>", url.QueryEscape("http://localhost:14000/appauth/token"))))
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"/appauth/password\">Password</a><br/>")))
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"/appauth/client_credentials\">Client Credentials</a><br/>")))
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"/appauth/assertion\">Assertion</a><br/>")))
|
||||
|
||||
w.Write([]byte("</body></html>"))
|
||||
})
|
||||
|
||||
// Application destination - CODE
|
||||
http.HandleFunc("/appauth/code", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
code := r.Form.Get("code")
|
||||
|
||||
w.Write([]byte("<html><body>"))
|
||||
w.Write([]byte("APP AUTH - CODE<br/>"))
|
||||
defer w.Write([]byte("</body></html>"))
|
||||
|
||||
if code == "" {
|
||||
w.Write([]byte("Nothing to do"))
|
||||
return
|
||||
}
|
||||
|
||||
jr := make(map[string]interface{})
|
||||
|
||||
// build access code url
|
||||
aurl := fmt.Sprintf("/token?grant_type=authorization_code&client_id=1234&client_secret=aabbccdd&state=xyz&redirect_uri=%s&code=%s",
|
||||
url.QueryEscape("http://localhost:14000/appauth/code"), url.QueryEscape(code))
|
||||
|
||||
// if parse, download and parse json
|
||||
if r.Form.Get("doparse") == "1" {
|
||||
err := example.DownloadAccessToken(fmt.Sprintf("http://localhost:14000%s", aurl),
|
||||
&osin.BasicAuth{"1234", "aabbccdd"}, jr)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
w.Write([]byte("<br/>"))
|
||||
}
|
||||
}
|
||||
|
||||
// show json error
|
||||
if erd, ok := jr["error"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ERROR: %s<br/>\n", erd)))
|
||||
}
|
||||
|
||||
// show json access token
|
||||
if at, ok := jr["access_token"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ACCESS TOKEN: %s<br/>\n", at)))
|
||||
}
|
||||
|
||||
w.Write([]byte(fmt.Sprintf("FULL RESULT: %+v<br/>\n", jr)))
|
||||
|
||||
// output links
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Goto Token URL</a><br/>", aurl)))
|
||||
|
||||
cururl := *r.URL
|
||||
curq := cururl.Query()
|
||||
curq.Add("doparse", "1")
|
||||
cururl.RawQuery = curq.Encode()
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Download Token</a><br/>", cururl.String())))
|
||||
|
||||
if rt, ok := jr["refresh_token"]; ok {
|
||||
rurl := fmt.Sprintf("/appauth/refresh?code=%s", rt)
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Refresh Token</a><br/>", rurl)))
|
||||
}
|
||||
|
||||
if at, ok := jr["access_token"]; ok {
|
||||
rurl := fmt.Sprintf("/appauth/info?code=%s", at)
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Info</a><br/>", rurl)))
|
||||
}
|
||||
})
|
||||
|
||||
// Application destination - TOKEN
|
||||
http.HandleFunc("/appauth/token", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
w.Write([]byte("<html><body>"))
|
||||
w.Write([]byte("APP AUTH - TOKEN<br/>"))
|
||||
|
||||
w.Write([]byte("Response data in fragment - not acessible via server - Nothing to do"))
|
||||
|
||||
w.Write([]byte("</body></html>"))
|
||||
})
|
||||
|
||||
// Application destination - PASSWORD
|
||||
http.HandleFunc("/appauth/password", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
w.Write([]byte("<html><body>"))
|
||||
w.Write([]byte("APP AUTH - PASSWORD<br/>"))
|
||||
|
||||
jr := make(map[string]interface{})
|
||||
|
||||
// build access code url
|
||||
aurl := fmt.Sprintf("/token?grant_type=password&scope=everything&username=%s&password=%s",
|
||||
"test", "test")
|
||||
|
||||
// download token
|
||||
err := example.DownloadAccessToken(fmt.Sprintf("http://localhost:14000%s", aurl),
|
||||
&osin.BasicAuth{Username: "1234", Password: "aabbccdd"}, jr)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
w.Write([]byte("<br/>"))
|
||||
}
|
||||
|
||||
// show json error
|
||||
if erd, ok := jr["error"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ERROR: %s<br/>\n", erd)))
|
||||
}
|
||||
|
||||
// show json access token
|
||||
if at, ok := jr["access_token"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ACCESS TOKEN: %s<br/>\n", at)))
|
||||
}
|
||||
|
||||
w.Write([]byte(fmt.Sprintf("FULL RESULT: %+v<br/>\n", jr)))
|
||||
|
||||
if rt, ok := jr["refresh_token"]; ok {
|
||||
rurl := fmt.Sprintf("/appauth/refresh?code=%s", rt)
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Refresh Token</a><br/>", rurl)))
|
||||
}
|
||||
|
||||
if at, ok := jr["access_token"]; ok {
|
||||
rurl := fmt.Sprintf("/appauth/info?code=%s", at)
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Info</a><br/>", rurl)))
|
||||
}
|
||||
|
||||
w.Write([]byte("</body></html>"))
|
||||
})
|
||||
|
||||
// Application destination - CLIENT_CREDENTIALS
|
||||
http.HandleFunc("/appauth/client_credentials", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
w.Write([]byte("<html><body>"))
|
||||
w.Write([]byte("APP AUTH - CLIENT CREDENTIALS<br/>"))
|
||||
|
||||
jr := make(map[string]interface{})
|
||||
|
||||
// build access code url
|
||||
aurl := fmt.Sprintf("/token?grant_type=client_credentials")
|
||||
|
||||
// download token
|
||||
err := example.DownloadAccessToken(fmt.Sprintf("http://localhost:14000%s", aurl),
|
||||
&osin.BasicAuth{Username: "1234", Password: "aabbccdd"}, jr)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
w.Write([]byte("<br/>"))
|
||||
}
|
||||
|
||||
// show json error
|
||||
if erd, ok := jr["error"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ERROR: %s<br/>\n", erd)))
|
||||
}
|
||||
|
||||
// show json access token
|
||||
if at, ok := jr["access_token"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ACCESS TOKEN: %s<br/>\n", at)))
|
||||
}
|
||||
|
||||
w.Write([]byte(fmt.Sprintf("FULL RESULT: %+v<br/>\n", jr)))
|
||||
|
||||
if rt, ok := jr["refresh_token"]; ok {
|
||||
rurl := fmt.Sprintf("/appauth/refresh?code=%s", rt)
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Refresh Token</a><br/>", rurl)))
|
||||
}
|
||||
|
||||
if at, ok := jr["access_token"]; ok {
|
||||
rurl := fmt.Sprintf("/appauth/info?code=%s", at)
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Info</a><br/>", rurl)))
|
||||
}
|
||||
|
||||
w.Write([]byte("</body></html>"))
|
||||
})
|
||||
|
||||
// Application destination - ASSERTION
|
||||
http.HandleFunc("/appauth/assertion", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
w.Write([]byte("<html><body>"))
|
||||
w.Write([]byte("APP AUTH - ASSERTION<br/>"))
|
||||
|
||||
jr := make(map[string]interface{})
|
||||
|
||||
// build access code url
|
||||
aurl := fmt.Sprintf("/token?grant_type=assertion&assertion_type=urn:osin.example.complete&assertion=osin.data")
|
||||
|
||||
// download token
|
||||
err := example.DownloadAccessToken(fmt.Sprintf("http://localhost:14000%s", aurl),
|
||||
&osin.BasicAuth{Username: "1234", Password: "aabbccdd"}, jr)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
w.Write([]byte("<br/>"))
|
||||
}
|
||||
|
||||
// show json error
|
||||
if erd, ok := jr["error"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ERROR: %s<br/>\n", erd)))
|
||||
}
|
||||
|
||||
// show json access token
|
||||
if at, ok := jr["access_token"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ACCESS TOKEN: %s<br/>\n", at)))
|
||||
}
|
||||
|
||||
w.Write([]byte(fmt.Sprintf("FULL RESULT: %+v<br/>\n", jr)))
|
||||
|
||||
if rt, ok := jr["refresh_token"]; ok {
|
||||
rurl := fmt.Sprintf("/appauth/refresh?code=%s", rt)
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Refresh Token</a><br/>", rurl)))
|
||||
}
|
||||
|
||||
if at, ok := jr["access_token"]; ok {
|
||||
rurl := fmt.Sprintf("/appauth/info?code=%s", at)
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Info</a><br/>", rurl)))
|
||||
}
|
||||
|
||||
w.Write([]byte("</body></html>"))
|
||||
})
|
||||
|
||||
// Application destination - REFRESH
|
||||
http.HandleFunc("/appauth/refresh", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
w.Write([]byte("<html><body>"))
|
||||
w.Write([]byte("APP AUTH - REFRESH<br/>"))
|
||||
defer w.Write([]byte("</body></html>"))
|
||||
|
||||
code := r.Form.Get("code")
|
||||
|
||||
if code == "" {
|
||||
w.Write([]byte("Nothing to do"))
|
||||
return
|
||||
}
|
||||
|
||||
jr := make(map[string]interface{})
|
||||
|
||||
// build access code url
|
||||
aurl := fmt.Sprintf("/token?grant_type=refresh_token&refresh_token=%s", url.QueryEscape(code))
|
||||
|
||||
// download token
|
||||
err := example.DownloadAccessToken(fmt.Sprintf("http://localhost:14000%s", aurl),
|
||||
&osin.BasicAuth{Username: "1234", Password: "aabbccdd"}, jr)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
w.Write([]byte("<br/>"))
|
||||
}
|
||||
|
||||
// show json error
|
||||
if erd, ok := jr["error"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ERROR: %s<br/>\n", erd)))
|
||||
}
|
||||
|
||||
// show json access token
|
||||
if at, ok := jr["access_token"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ACCESS TOKEN: %s<br/>\n", at)))
|
||||
}
|
||||
|
||||
w.Write([]byte(fmt.Sprintf("FULL RESULT: %+v<br/>\n", jr)))
|
||||
|
||||
if rt, ok := jr["refresh_token"]; ok {
|
||||
rurl := fmt.Sprintf("/appauth/refresh?code=%s", rt)
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Refresh Token</a><br/>", rurl)))
|
||||
}
|
||||
|
||||
if at, ok := jr["access_token"]; ok {
|
||||
rurl := fmt.Sprintf("/appauth/info?code=%s", at)
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Info</a><br/>", rurl)))
|
||||
}
|
||||
})
|
||||
|
||||
// Application destination - INFO
|
||||
http.HandleFunc("/appauth/info", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
w.Write([]byte("<html><body>"))
|
||||
w.Write([]byte("APP AUTH - INFO<br/>"))
|
||||
defer w.Write([]byte("</body></html>"))
|
||||
|
||||
code := r.Form.Get("code")
|
||||
|
||||
if code == "" {
|
||||
w.Write([]byte("Nothing to do"))
|
||||
return
|
||||
}
|
||||
|
||||
jr := make(map[string]interface{})
|
||||
|
||||
// build access code url
|
||||
aurl := fmt.Sprintf("/info?code=%s", url.QueryEscape(code))
|
||||
|
||||
// download token
|
||||
err := example.DownloadAccessToken(fmt.Sprintf("http://localhost:14000%s", aurl),
|
||||
&osin.BasicAuth{Username: "1234", Password: "aabbccdd"}, jr)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
w.Write([]byte("<br/>"))
|
||||
}
|
||||
|
||||
// show json error
|
||||
if erd, ok := jr["error"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ERROR: %s<br/>\n", erd)))
|
||||
}
|
||||
|
||||
// show json access token
|
||||
if at, ok := jr["access_token"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ACCESS TOKEN: %s<br/>\n", at)))
|
||||
}
|
||||
|
||||
w.Write([]byte(fmt.Sprintf("FULL RESULT: %+v<br/>\n", jr)))
|
||||
|
||||
if rt, ok := jr["refresh_token"]; ok {
|
||||
rurl := fmt.Sprintf("/appauth/refresh?code=%s", rt)
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Refresh Token</a><br/>", rurl)))
|
||||
}
|
||||
})
|
||||
|
||||
http.ListenAndServe(":14000", nil)
|
||||
}
|
130
vendor/github.com/RangelReale/osin/example/goauth2client/goauth2client.go
generated
vendored
Normal file
130
vendor/github.com/RangelReale/osin/example/goauth2client/goauth2client.go
generated
vendored
Normal file
@@ -0,0 +1,130 @@
|
||||
package main
|
||||
|
||||
// Use golang.org/x/oauth2 client to test
|
||||
// Open url in browser:
|
||||
// http://localhost:14000/app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/RangelReale/osin"
|
||||
"github.com/RangelReale/osin/example"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
config := osin.NewServerConfig()
|
||||
// goauth2 checks errors using status codes
|
||||
config.ErrorStatusCode = 401
|
||||
|
||||
server := osin.NewServer(config, example.NewTestStorage())
|
||||
|
||||
client := &oauth2.Config{
|
||||
ClientID: "1234",
|
||||
ClientSecret: "aabbccdd",
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: "http://localhost:14000/authorize",
|
||||
TokenURL: "http://localhost:14000/token",
|
||||
},
|
||||
RedirectURL: "http://localhost:14000/appauth/code",
|
||||
}
|
||||
|
||||
// Authorization code endpoint
|
||||
http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ar := server.HandleAuthorizeRequest(resp, r); ar != nil {
|
||||
if !example.HandleLoginPage(ar, w, r) {
|
||||
return
|
||||
}
|
||||
ar.Authorized = true
|
||||
server.FinishAuthorizeRequest(resp, r, ar)
|
||||
}
|
||||
if resp.IsError && resp.InternalError != nil {
|
||||
fmt.Printf("ERROR: %s\n", resp.InternalError)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// Access token endpoint
|
||||
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ar := server.HandleAccessRequest(resp, r); ar != nil {
|
||||
ar.Authorized = true
|
||||
server.FinishAccessRequest(resp, r, ar)
|
||||
}
|
||||
if resp.IsError && resp.InternalError != nil {
|
||||
fmt.Printf("ERROR: %s\n", resp.InternalError)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// Information endpoint
|
||||
http.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ir := server.HandleInfoRequest(resp, r); ir != nil {
|
||||
server.FinishInfoRequest(resp, r, ir)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// Application home endpoint
|
||||
http.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("<html><body>"))
|
||||
//w.Write([]byte(fmt.Sprintf("<a href=\"/authorize?response_type=code&client_id=1234&state=xyz&scope=everything&redirect_uri=%s\">Login</a><br/>", url.QueryEscape("http://localhost:14000/appauth/code"))))
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Login</a><br/>", client.AuthCodeURL(""))))
|
||||
w.Write([]byte("</body></html>"))
|
||||
})
|
||||
|
||||
// Application destination - CODE
|
||||
http.HandleFunc("/appauth/code", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
code := r.Form.Get("code")
|
||||
|
||||
w.Write([]byte("<html><body>"))
|
||||
w.Write([]byte("APP AUTH - CODE<br/>"))
|
||||
defer w.Write([]byte("</body></html>"))
|
||||
|
||||
if code == "" {
|
||||
w.Write([]byte("Nothing to do"))
|
||||
return
|
||||
}
|
||||
|
||||
var jr *oauth2.Token
|
||||
var err error
|
||||
|
||||
// if parse, download and parse json
|
||||
if r.Form.Get("doparse") == "1" {
|
||||
jr, err = client.Exchange(oauth2.NoContext, code)
|
||||
if err != nil {
|
||||
jr = nil
|
||||
w.Write([]byte(fmt.Sprintf("ERROR: %s<br/>\n", err)))
|
||||
}
|
||||
}
|
||||
|
||||
// show json access token
|
||||
if jr != nil {
|
||||
w.Write([]byte(fmt.Sprintf("ACCESS TOKEN: %s<br/>\n", jr.AccessToken)))
|
||||
if jr.RefreshToken != "" {
|
||||
w.Write([]byte(fmt.Sprintf("REFRESH TOKEN: %s<br/>\n", jr.RefreshToken)))
|
||||
}
|
||||
}
|
||||
|
||||
w.Write([]byte(fmt.Sprintf("FULL RESULT: %+v<br/>\n", jr)))
|
||||
|
||||
cururl := *r.URL
|
||||
curq := cururl.Query()
|
||||
curq.Add("doparse", "1")
|
||||
cururl.RawQuery = curq.Encode()
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Download Token</a><br/>", cururl.String())))
|
||||
})
|
||||
|
||||
http.ListenAndServe(":14000", nil)
|
||||
}
|
57
vendor/github.com/RangelReale/osin/example/helper.go
generated
vendored
Normal file
57
vendor/github.com/RangelReale/osin/example/helper.go
generated
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/RangelReale/osin"
|
||||
)
|
||||
|
||||
func HandleLoginPage(ar *osin.AuthorizeRequest, w http.ResponseWriter, r *http.Request) bool {
|
||||
r.ParseForm()
|
||||
if r.Method == "POST" && r.Form.Get("login") == "test" && r.Form.Get("password") == "test" {
|
||||
return true
|
||||
}
|
||||
|
||||
w.Write([]byte("<html><body>"))
|
||||
|
||||
w.Write([]byte(fmt.Sprintf("LOGIN %s (use test/test)<br/>", ar.Client.GetId())))
|
||||
w.Write([]byte(fmt.Sprintf("<form action=\"/authorize?%s\" method=\"POST\">", r.URL.RawQuery)))
|
||||
|
||||
w.Write([]byte("Login: <input type=\"text\" name=\"login\" /><br/>"))
|
||||
w.Write([]byte("Password: <input type=\"password\" name=\"password\" /><br/>"))
|
||||
w.Write([]byte("<input type=\"submit\"/>"))
|
||||
|
||||
w.Write([]byte("</form>"))
|
||||
|
||||
w.Write([]byte("</body></html>"))
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func DownloadAccessToken(url string, auth *osin.BasicAuth, output map[string]interface{}) error {
|
||||
// download access token
|
||||
preq, err := http.NewRequest("POST", url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if auth != nil {
|
||||
preq.SetBasicAuth(auth.Username, auth.Password)
|
||||
}
|
||||
|
||||
pclient := &http.Client{}
|
||||
presp, err := pclient.Do(preq)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if presp.StatusCode != 200 {
|
||||
return errors.New("Invalid status code")
|
||||
}
|
||||
|
||||
jdec := json.NewDecoder(presp.Body)
|
||||
err = jdec.Decode(&output)
|
||||
return err
|
||||
}
|
215
vendor/github.com/RangelReale/osin/example/jwttoken/jwttoken.go
generated
vendored
Normal file
215
vendor/github.com/RangelReale/osin/example/jwttoken/jwttoken.go
generated
vendored
Normal file
@@ -0,0 +1,215 @@
|
||||
package main
|
||||
|
||||
// Open url in browser:
|
||||
// http://localhost:14000/app
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/RangelReale/osin"
|
||||
"github.com/RangelReale/osin/example"
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
)
|
||||
|
||||
// JWT access token generator
|
||||
type AccessTokenGenJWT struct {
|
||||
PrivateKey *rsa.PrivateKey
|
||||
PublicKey *rsa.PublicKey
|
||||
}
|
||||
|
||||
func (c *AccessTokenGenJWT) GenerateAccessToken(data *osin.AccessData, generaterefresh bool) (accesstoken string, refreshtoken string, err error) {
|
||||
// generate JWT access token
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
|
||||
"cid": data.Client.GetId(),
|
||||
"exp": data.ExpireAt().Unix(),
|
||||
})
|
||||
|
||||
accesstoken, err = token.SignedString(c.PrivateKey)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
if !generaterefresh {
|
||||
return
|
||||
}
|
||||
|
||||
// generate JWT refresh token
|
||||
token = jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
|
||||
"cid": data.Client.GetId(),
|
||||
})
|
||||
|
||||
refreshtoken, err = token.SignedString(c.PrivateKey)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
server := osin.NewServer(osin.NewServerConfig(), example.NewTestStorage())
|
||||
|
||||
var err error
|
||||
var accessTokenGenJWT AccessTokenGenJWT
|
||||
|
||||
if accessTokenGenJWT.PrivateKey, err = jwt.ParseRSAPrivateKeyFromPEM(privatekeyPEM); err != nil {
|
||||
fmt.Printf("ERROR: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if accessTokenGenJWT.PublicKey, err = jwt.ParseRSAPublicKeyFromPEM(publickeyPEM); err != nil {
|
||||
fmt.Printf("ERROR: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
server.AccessTokenGen = &accessTokenGenJWT
|
||||
|
||||
// Authorization code endpoint
|
||||
http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ar := server.HandleAuthorizeRequest(resp, r); ar != nil {
|
||||
if !example.HandleLoginPage(ar, w, r) {
|
||||
return
|
||||
}
|
||||
ar.Authorized = true
|
||||
server.FinishAuthorizeRequest(resp, r, ar)
|
||||
}
|
||||
if resp.IsError && resp.InternalError != nil {
|
||||
fmt.Printf("ERROR: %s\n", resp.InternalError)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// Access token endpoint
|
||||
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ar := server.HandleAccessRequest(resp, r); ar != nil {
|
||||
ar.Authorized = true
|
||||
server.FinishAccessRequest(resp, r, ar)
|
||||
}
|
||||
if resp.IsError && resp.InternalError != nil {
|
||||
fmt.Printf("ERROR: %s\n", resp.InternalError)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// Information endpoint
|
||||
http.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ir := server.HandleInfoRequest(resp, r); ir != nil {
|
||||
server.FinishInfoRequest(resp, r, ir)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// Application home endpoint
|
||||
http.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("<html><body>"))
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"/authorize?response_type=code&client_id=1234&state=xyz&scope=everything&redirect_uri=%s\">Login</a><br/>", url.QueryEscape("http://localhost:14000/appauth/code"))))
|
||||
w.Write([]byte("</body></html>"))
|
||||
})
|
||||
|
||||
// Application destination - CODE
|
||||
http.HandleFunc("/appauth/code", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
code := r.Form.Get("code")
|
||||
|
||||
w.Write([]byte("<html><body>"))
|
||||
w.Write([]byte("APP AUTH - CODE<br/>"))
|
||||
defer w.Write([]byte("</body></html>"))
|
||||
|
||||
if code == "" {
|
||||
w.Write([]byte("Nothing to do"))
|
||||
return
|
||||
}
|
||||
|
||||
jr := make(map[string]interface{})
|
||||
|
||||
// build access code url
|
||||
aurl := fmt.Sprintf("/token?grant_type=authorization_code&client_id=1234&state=xyz&redirect_uri=%s&code=%s",
|
||||
url.QueryEscape("http://localhost:14000/appauth/code"), url.QueryEscape(code))
|
||||
|
||||
// if parse, download and parse json
|
||||
if r.Form.Get("doparse") == "1" {
|
||||
err := example.DownloadAccessToken(fmt.Sprintf("http://localhost:14000%s", aurl),
|
||||
&osin.BasicAuth{"1234", "aabbccdd"}, jr)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
w.Write([]byte("<br/>"))
|
||||
}
|
||||
}
|
||||
|
||||
// show json error
|
||||
if erd, ok := jr["error"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ERROR: %s<br/>\n", erd)))
|
||||
}
|
||||
|
||||
// show json access token
|
||||
if at, ok := jr["access_token"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ACCESS TOKEN: %s<br/>\n", at)))
|
||||
}
|
||||
|
||||
w.Write([]byte(fmt.Sprintf("FULL RESULT: %+v<br/>\n", jr)))
|
||||
|
||||
// output links
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Goto Token URL</a><br/>", aurl)))
|
||||
|
||||
cururl := *r.URL
|
||||
curq := cururl.Query()
|
||||
curq.Add("doparse", "1")
|
||||
cururl.RawQuery = curq.Encode()
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Download Token</a><br/>", cururl.String())))
|
||||
})
|
||||
|
||||
http.ListenAndServe(":14000", nil)
|
||||
}
|
||||
|
||||
var (
|
||||
privatekeyPEM = []byte(`-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEowIBAAKCAQEA4f5wg5l2hKsTeNem/V41fGnJm6gOdrj8ym3rFkEU/wT8RDtn
|
||||
SgFEZOQpHEgQ7JL38xUfU0Y3g6aYw9QT0hJ7mCpz9Er5qLaMXJwZxzHzAahlfA0i
|
||||
cqabvJOMvQtzD6uQv6wPEyZtDTWiQi9AXwBpHssPnpYGIn20ZZuNlX2BrClciHhC
|
||||
PUIIZOQn/MmqTD31jSyjoQoV7MhhMTATKJx2XrHhR+1DcKJzQBSTAGnpYVaqpsAR
|
||||
ap+nwRipr3nUTuxyGohBTSmjJ2usSeQXHI3bODIRe1AuTyHceAbewn8b462yEWKA
|
||||
Rdpd9AjQW5SIVPfdsz5B6GlYQ5LdYKtznTuy7wIDAQABAoIBAQCwia1k7+2oZ2d3
|
||||
n6agCAbqIE1QXfCmh41ZqJHbOY3oRQG3X1wpcGH4Gk+O+zDVTV2JszdcOt7E5dAy
|
||||
MaomETAhRxB7hlIOnEN7WKm+dGNrKRvV0wDU5ReFMRHg31/Lnu8c+5BvGjZX+ky9
|
||||
POIhFFYJqwCRlopGSUIxmVj5rSgtzk3iWOQXr+ah1bjEXvlxDOWkHN6YfpV5ThdE
|
||||
KdBIPGEVqa63r9n2h+qazKrtiRqJqGnOrHzOECYbRFYhexsNFz7YT02xdfSHn7gM
|
||||
IvabDDP/Qp0PjE1jdouiMaFHYnLBbgvlnZW9yuVf/rpXTUq/njxIXMmvmEyyvSDn
|
||||
FcFikB8pAoGBAPF77hK4m3/rdGT7X8a/gwvZ2R121aBcdPwEaUhvj/36dx596zvY
|
||||
mEOjrWfZhF083/nYWE2kVquj2wjs+otCLfifEEgXcVPTnEOPO9Zg3uNSL0nNQghj
|
||||
FuD3iGLTUBCtM66oTe0jLSslHe8gLGEQqyMzHOzYxNqibxcOZIe8Qt0NAoGBAO+U
|
||||
I5+XWjWEgDmvyC3TrOSf/KCGjtu0TSv30ipv27bDLMrpvPmD/5lpptTFwcxvVhCs
|
||||
2b+chCjlghFSWFbBULBrfci2FtliClOVMYrlNBdUSJhf3aYSG2Doe6Bgt1n2CpNn
|
||||
/iu37Y3NfemZBJA7hNl4dYe+f+uzM87cdQ214+jrAoGAXA0XxX8ll2+ToOLJsaNT
|
||||
OvNB9h9Uc5qK5X5w+7G7O998BN2PC/MWp8H+2fVqpXgNENpNXttkRm1hk1dych86
|
||||
EunfdPuqsX+as44oCyJGFHVBnWpm33eWQw9YqANRI+pCJzP08I5WK3osnPiwshd+
|
||||
hR54yjgfYhBFNI7B95PmEQkCgYBzFSz7h1+s34Ycr8SvxsOBWxymG5zaCsUbPsL0
|
||||
4aCgLScCHb9J+E86aVbbVFdglYa5Id7DPTL61ixhl7WZjujspeXZGSbmq0Kcnckb
|
||||
mDgqkLECiOJW2NHP/j0McAkDLL4tysF8TLDO8gvuvzNC+WQ6drO2ThrypLVZQ+ry
|
||||
eBIPmwKBgEZxhqa0gVvHQG/7Od69KWj4eJP28kq13RhKay8JOoN0vPmspXJo1HY3
|
||||
CKuHRG+AP579dncdUnOMvfXOtkdM4vk0+hWASBQzM9xzVcztCa+koAugjVaLS9A+
|
||||
9uQoqEeVNTckxx0S2bYevRy7hGQmUJTyQm3j1zEUR5jpdbL83Fbq
|
||||
-----END RSA PRIVATE KEY-----`)
|
||||
|
||||
publickeyPEM = []byte(`-----BEGIN PUBLIC KEY-----
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4f5wg5l2hKsTeNem/V41
|
||||
fGnJm6gOdrj8ym3rFkEU/wT8RDtnSgFEZOQpHEgQ7JL38xUfU0Y3g6aYw9QT0hJ7
|
||||
mCpz9Er5qLaMXJwZxzHzAahlfA0icqabvJOMvQtzD6uQv6wPEyZtDTWiQi9AXwBp
|
||||
HssPnpYGIn20ZZuNlX2BrClciHhCPUIIZOQn/MmqTD31jSyjoQoV7MhhMTATKJx2
|
||||
XrHhR+1DcKJzQBSTAGnpYVaqpsARap+nwRipr3nUTuxyGohBTSmjJ2usSeQXHI3b
|
||||
ODIRe1AuTyHceAbewn8b462yEWKARdpd9AjQW5SIVPfdsz5B6GlYQ5LdYKtznTuy
|
||||
7wIDAQAB
|
||||
-----END PUBLIC KEY-----`)
|
||||
)
|
273
vendor/github.com/RangelReale/osin/example/openidconnect/openidconnect.go
generated
vendored
Normal file
273
vendor/github.com/RangelReale/osin/example/openidconnect/openidconnect.go
generated
vendored
Normal file
@@ -0,0 +1,273 @@
|
||||
/*
|
||||
An example of adding OpenID Connect support to osin.
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/RangelReale/osin"
|
||||
"github.com/RangelReale/osin/example"
|
||||
|
||||
"gopkg.in/square/go-jose.v1"
|
||||
)
|
||||
|
||||
var (
|
||||
issuer = "http://127.0.0.1:14001"
|
||||
server = osin.NewServer(osin.NewServerConfig(), example.NewTestStorage())
|
||||
|
||||
jwtSigner jose.Signer
|
||||
publicKeys *jose.JsonWebKeySet
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Load signing key.
|
||||
block, _ := pem.Decode(privateKeyBytes)
|
||||
if block == nil {
|
||||
log.Fatalf("no private key found")
|
||||
}
|
||||
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to parse key: %v", err)
|
||||
}
|
||||
|
||||
// Configure jwtSigner and public keys.
|
||||
privateKey := &jose.JsonWebKey{
|
||||
Key: key,
|
||||
Algorithm: "RS256",
|
||||
Use: "sig",
|
||||
KeyID: "1", // KeyID should use the key thumbprint.
|
||||
}
|
||||
|
||||
jwtSigner, err = jose.NewSigner(jose.RS256, privateKey)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create jwtSigner: %v", err)
|
||||
}
|
||||
publicKeys = &jose.JsonWebKeySet{
|
||||
Keys: []jose.JsonWebKey{
|
||||
jose.JsonWebKey{Key: &key.PublicKey,
|
||||
Algorithm: "RS256",
|
||||
Use: "sig",
|
||||
KeyID: "1",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Register the four manditory OpenID Connect endpoints: discovery, public keys, auth, and token.
|
||||
http.HandleFunc("/.well-known/openid-configuration", handleDiscovery)
|
||||
http.HandleFunc("/publickeys", handlePublicKeys)
|
||||
http.HandleFunc("/authorize", handleAuthorization)
|
||||
http.HandleFunc("/token", handleToken)
|
||||
|
||||
log.Fatal(http.ListenAndServe("127.0.0.1:14001", nil))
|
||||
}
|
||||
|
||||
// The ID Token represents a JWT passed to the client as part of the token response.
|
||||
//
|
||||
// https://openid.net/specs/openid-connect-core-1_0.html#IDToken
|
||||
type IDToken struct {
|
||||
Issuer string `json:"iss"`
|
||||
UserID string `json:"sub"`
|
||||
ClientID string `json:"aud"`
|
||||
Expiration int64 `json:"exp"`
|
||||
IssuedAt int64 `json:"iat"`
|
||||
|
||||
Nonce string `json:"nonce,omitempty"` // Non-manditory fields MUST be "omitempty"
|
||||
|
||||
// Custom claims supported by this server.
|
||||
//
|
||||
// See: https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
|
||||
|
||||
Email string `json:"email,omitempty"`
|
||||
EmailVerified *bool `json:"email_verified,omitempty"`
|
||||
|
||||
Name string `json:"name,omitempty"`
|
||||
FamilyName string `json:"family_name,omitempty"`
|
||||
GivenName string `json:"given_name,omitempty"`
|
||||
Locale string `json:"locale,omitempty"`
|
||||
}
|
||||
|
||||
// handleDiscovery returns the OpenID Connect discovery object, allowing clients
|
||||
// to discover OAuth2 resources.
|
||||
func handleDiscovery(w http.ResponseWriter, r *http.Request) {
|
||||
// For other example see: https://accounts.google.com/.well-known/openid-configuration
|
||||
data := map[string]interface{}{
|
||||
"issuer": issuer,
|
||||
"authorization_endpoint": issuer + "/authorize",
|
||||
"token_endpoint": issuer + "/token",
|
||||
"jwks_uri": issuer + "/publickeys",
|
||||
"response_types_supported": []string{"code"},
|
||||
"subject_types_supported": []string{"public"},
|
||||
"id_token_signing_alg_values_supported": []string{"RS256"},
|
||||
"scopes_supported": []string{"openid", "email", "profile"},
|
||||
"token_endpoint_auth_methods_supported": []string{"client_secret_basic"},
|
||||
"claims_supported": []string{
|
||||
"aud", "email", "email_verified", "exp",
|
||||
"family_name", "given_name", "iat", "iss",
|
||||
"locale", "name", "sub",
|
||||
},
|
||||
}
|
||||
|
||||
raw, err := json.MarshalIndent(data, "", " ")
|
||||
if err != nil {
|
||||
log.Printf("failed to marshal data: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(raw)))
|
||||
w.Write(raw)
|
||||
}
|
||||
|
||||
// handlePublicKeys publishes the public part of this server's signing keys.
|
||||
// This allows clients to verify the signature of ID Tokens.
|
||||
func handlePublicKeys(w http.ResponseWriter, r *http.Request) {
|
||||
raw, err := json.MarshalIndent(publicKeys, "", " ")
|
||||
if err != nil {
|
||||
log.Printf("failed to marshal data: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(raw)))
|
||||
w.Write(raw)
|
||||
}
|
||||
|
||||
func handleAuthorization(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ar := server.HandleAuthorizeRequest(resp, r); ar != nil {
|
||||
if !example.HandleLoginPage(ar, w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
ar.Authorized = true
|
||||
scopes := make(map[string]bool)
|
||||
for _, s := range strings.Fields(ar.Scope) {
|
||||
scopes[s] = true
|
||||
}
|
||||
|
||||
// If the "openid" connect scope is specified, attach an ID Token to the
|
||||
// authorization response.
|
||||
//
|
||||
// The ID Token will be serialized and signed during the code for token exchange.
|
||||
if scopes["openid"] {
|
||||
|
||||
// These values would be tied to the end user authorizing the client.
|
||||
now := time.Now()
|
||||
idToken := IDToken{
|
||||
Issuer: issuer,
|
||||
UserID: "id-of-test-user",
|
||||
ClientID: ar.Client.GetId(),
|
||||
Expiration: now.Add(time.Hour).Unix(),
|
||||
IssuedAt: now.Unix(),
|
||||
Nonce: r.URL.Query().Get("nonce"),
|
||||
}
|
||||
|
||||
if scopes["profile"] {
|
||||
idToken.Name = "Jane Doe"
|
||||
idToken.GivenName = "Jane"
|
||||
idToken.FamilyName = "Doe"
|
||||
idToken.Locale = "us"
|
||||
}
|
||||
|
||||
if scopes["email"] {
|
||||
t := true
|
||||
idToken.Email = "jane.doe@example.com"
|
||||
idToken.EmailVerified = &t
|
||||
}
|
||||
// NOTE: The storage must be able to encode and decode this object.
|
||||
ar.UserData = &idToken
|
||||
}
|
||||
server.FinishAuthorizeRequest(resp, r, ar)
|
||||
}
|
||||
|
||||
if resp.IsError && resp.InternalError != nil {
|
||||
log.Printf("internal error: %v", resp.InternalError)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
}
|
||||
|
||||
func handleToken(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ar := server.HandleAccessRequest(resp, r); ar != nil {
|
||||
ar.Authorized = true
|
||||
server.FinishAccessRequest(resp, r, ar)
|
||||
|
||||
// If an ID Token was encoded as the UserData, serialize and sign it.
|
||||
if idToken, ok := ar.UserData.(*IDToken); ok && idToken != nil {
|
||||
encodeIDToken(resp, idToken, jwtSigner)
|
||||
}
|
||||
}
|
||||
if resp.IsError && resp.InternalError != nil {
|
||||
fmt.Printf("ERROR: %s\n", resp.InternalError)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
}
|
||||
|
||||
// encodeIDToken serializes and signs an ID Token then adds a field to the token response.
|
||||
func encodeIDToken(resp *osin.Response, idToken *IDToken, singer jose.Signer) {
|
||||
resp.InternalError = func() error {
|
||||
payload, err := json.Marshal(idToken)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal token: %v", err)
|
||||
}
|
||||
jws, err := jwtSigner.Sign(payload)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to sign token: %v", err)
|
||||
}
|
||||
raw, err := jws.CompactSerialize()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to serialize token: %v", err)
|
||||
}
|
||||
resp.Output["id_token"] = raw
|
||||
return nil
|
||||
}()
|
||||
|
||||
// Record errors as internal server errors.
|
||||
if resp.InternalError != nil {
|
||||
resp.IsError = true
|
||||
resp.ErrorId = osin.E_SERVER_ERROR
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
privateKeyBytes = []byte(`-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEowIBAAKCAQEA4f5wg5l2hKsTeNem/V41fGnJm6gOdrj8ym3rFkEU/wT8RDtn
|
||||
SgFEZOQpHEgQ7JL38xUfU0Y3g6aYw9QT0hJ7mCpz9Er5qLaMXJwZxzHzAahlfA0i
|
||||
cqabvJOMvQtzD6uQv6wPEyZtDTWiQi9AXwBpHssPnpYGIn20ZZuNlX2BrClciHhC
|
||||
PUIIZOQn/MmqTD31jSyjoQoV7MhhMTATKJx2XrHhR+1DcKJzQBSTAGnpYVaqpsAR
|
||||
ap+nwRipr3nUTuxyGohBTSmjJ2usSeQXHI3bODIRe1AuTyHceAbewn8b462yEWKA
|
||||
Rdpd9AjQW5SIVPfdsz5B6GlYQ5LdYKtznTuy7wIDAQABAoIBAQCwia1k7+2oZ2d3
|
||||
n6agCAbqIE1QXfCmh41ZqJHbOY3oRQG3X1wpcGH4Gk+O+zDVTV2JszdcOt7E5dAy
|
||||
MaomETAhRxB7hlIOnEN7WKm+dGNrKRvV0wDU5ReFMRHg31/Lnu8c+5BvGjZX+ky9
|
||||
POIhFFYJqwCRlopGSUIxmVj5rSgtzk3iWOQXr+ah1bjEXvlxDOWkHN6YfpV5ThdE
|
||||
KdBIPGEVqa63r9n2h+qazKrtiRqJqGnOrHzOECYbRFYhexsNFz7YT02xdfSHn7gM
|
||||
IvabDDP/Qp0PjE1jdouiMaFHYnLBbgvlnZW9yuVf/rpXTUq/njxIXMmvmEyyvSDn
|
||||
FcFikB8pAoGBAPF77hK4m3/rdGT7X8a/gwvZ2R121aBcdPwEaUhvj/36dx596zvY
|
||||
mEOjrWfZhF083/nYWE2kVquj2wjs+otCLfifEEgXcVPTnEOPO9Zg3uNSL0nNQghj
|
||||
FuD3iGLTUBCtM66oTe0jLSslHe8gLGEQqyMzHOzYxNqibxcOZIe8Qt0NAoGBAO+U
|
||||
I5+XWjWEgDmvyC3TrOSf/KCGjtu0TSv30ipv27bDLMrpvPmD/5lpptTFwcxvVhCs
|
||||
2b+chCjlghFSWFbBULBrfci2FtliClOVMYrlNBdUSJhf3aYSG2Doe6Bgt1n2CpNn
|
||||
/iu37Y3NfemZBJA7hNl4dYe+f+uzM87cdQ214+jrAoGAXA0XxX8ll2+ToOLJsaNT
|
||||
OvNB9h9Uc5qK5X5w+7G7O998BN2PC/MWp8H+2fVqpXgNENpNXttkRm1hk1dych86
|
||||
EunfdPuqsX+as44oCyJGFHVBnWpm33eWQw9YqANRI+pCJzP08I5WK3osnPiwshd+
|
||||
hR54yjgfYhBFNI7B95PmEQkCgYBzFSz7h1+s34Ycr8SvxsOBWxymG5zaCsUbPsL0
|
||||
4aCgLScCHb9J+E86aVbbVFdglYa5Id7DPTL61ixhl7WZjujspeXZGSbmq0Kcnckb
|
||||
mDgqkLECiOJW2NHP/j0McAkDLL4tysF8TLDO8gvuvzNC+WQ6drO2ThrypLVZQ+ry
|
||||
eBIPmwKBgEZxhqa0gVvHQG/7Od69KWj4eJP28kq13RhKay8JOoN0vPmspXJo1HY3
|
||||
CKuHRG+AP579dncdUnOMvfXOtkdM4vk0+hWASBQzM9xzVcztCa+koAugjVaLS9A+
|
||||
9uQoqEeVNTckxx0S2bYevRy7hGQmUJTyQm3j1zEUR5jpdbL83Fbq
|
||||
-----END RSA PRIVATE KEY-----`)
|
||||
)
|
127
vendor/github.com/RangelReale/osin/example/osincliclient/osincliclient.go
generated
vendored
Normal file
127
vendor/github.com/RangelReale/osin/example/osincliclient/osincliclient.go
generated
vendored
Normal file
@@ -0,0 +1,127 @@
|
||||
package main
|
||||
|
||||
// Use github.com/RangelReale/osincli client to test
|
||||
// Open url in browser:
|
||||
// http://localhost:14001
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/RangelReale/osin"
|
||||
"github.com/RangelReale/osin/example"
|
||||
"github.com/RangelReale/osincli"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// create http muxes
|
||||
serverhttp := http.NewServeMux()
|
||||
clienthttp := http.NewServeMux()
|
||||
|
||||
// create server
|
||||
config := osin.NewServerConfig()
|
||||
sstorage := example.NewTestStorage()
|
||||
sstorage.SetClient("1234", &osin.DefaultClient{
|
||||
Id: "1234",
|
||||
Secret: "aabbccdd",
|
||||
RedirectUri: "http://localhost:14001/appauth",
|
||||
})
|
||||
server := osin.NewServer(config, sstorage)
|
||||
|
||||
// create client
|
||||
cliconfig := &osincli.ClientConfig{
|
||||
ClientId: "1234",
|
||||
ClientSecret: "aabbccdd",
|
||||
AuthorizeUrl: "http://localhost:14000/authorize",
|
||||
TokenUrl: "http://localhost:14000/token",
|
||||
RedirectUrl: "http://localhost:14001/appauth",
|
||||
}
|
||||
client, err := osincli.NewClient(cliconfig)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// create a new request to generate the url
|
||||
areq := client.NewAuthorizeRequest(osincli.CODE)
|
||||
|
||||
// SERVER
|
||||
|
||||
// Authorization code endpoint
|
||||
serverhttp.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ar := server.HandleAuthorizeRequest(resp, r); ar != nil {
|
||||
if !example.HandleLoginPage(ar, w, r) {
|
||||
return
|
||||
}
|
||||
ar.Authorized = true
|
||||
server.FinishAuthorizeRequest(resp, r, ar)
|
||||
}
|
||||
if resp.IsError && resp.InternalError != nil {
|
||||
fmt.Printf("ERROR: %s\n", resp.InternalError)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// Access token endpoint
|
||||
serverhttp.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ar := server.HandleAccessRequest(resp, r); ar != nil {
|
||||
ar.Authorized = true
|
||||
server.FinishAccessRequest(resp, r, ar)
|
||||
}
|
||||
if resp.IsError && resp.InternalError != nil {
|
||||
fmt.Printf("ERROR: %s\n", resp.InternalError)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// Information endpoint
|
||||
serverhttp.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ir := server.HandleInfoRequest(resp, r); ir != nil {
|
||||
server.FinishInfoRequest(resp, r, ir)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// CLIENT
|
||||
|
||||
// Home
|
||||
clienthttp.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
u := areq.GetAuthorizeUrl()
|
||||
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Login</a>", u.String())))
|
||||
})
|
||||
|
||||
// Auth endpoint
|
||||
clienthttp.HandleFunc("/appauth", func(w http.ResponseWriter, r *http.Request) {
|
||||
// parse a token request
|
||||
areqdata, err := areq.HandleRequest(r)
|
||||
if err != nil {
|
||||
w.Write([]byte(fmt.Sprintf("ERROR: %s\n", err)))
|
||||
return
|
||||
}
|
||||
|
||||
treq := client.NewAccessRequest(osincli.AUTHORIZATION_CODE, areqdata)
|
||||
|
||||
// show access request url (for debugging only)
|
||||
u2 := treq.GetTokenUrl()
|
||||
w.Write([]byte(fmt.Sprintf("Access token URL: %s\n", u2.String())))
|
||||
|
||||
// exchange the authorize token for the access token
|
||||
ad, err := treq.GetToken()
|
||||
if err != nil {
|
||||
w.Write([]byte(fmt.Sprintf("ERROR: %s\n", err)))
|
||||
return
|
||||
}
|
||||
w.Write([]byte(fmt.Sprintf("Access token: %+v\n", ad)))
|
||||
})
|
||||
|
||||
go http.ListenAndServe(":14001", clienthttp)
|
||||
http.ListenAndServe(":14000", serverhttp)
|
||||
}
|
126
vendor/github.com/RangelReale/osin/example/simple/simple.go
generated
vendored
Normal file
126
vendor/github.com/RangelReale/osin/example/simple/simple.go
generated
vendored
Normal file
@@ -0,0 +1,126 @@
|
||||
package main
|
||||
|
||||
// Open url in browser:
|
||||
// http://localhost:14000/app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/RangelReale/osin"
|
||||
"github.com/RangelReale/osin/example"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg := osin.NewServerConfig()
|
||||
cfg.AllowGetAccessRequest = true
|
||||
cfg.AllowClientSecretInParams = true
|
||||
|
||||
server := osin.NewServer(cfg, example.NewTestStorage())
|
||||
|
||||
// Authorization code endpoint
|
||||
http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ar := server.HandleAuthorizeRequest(resp, r); ar != nil {
|
||||
if !example.HandleLoginPage(ar, w, r) {
|
||||
return
|
||||
}
|
||||
ar.Authorized = true
|
||||
server.FinishAuthorizeRequest(resp, r, ar)
|
||||
}
|
||||
if resp.IsError && resp.InternalError != nil {
|
||||
fmt.Printf("ERROR: %s\n", resp.InternalError)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// Access token endpoint
|
||||
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ar := server.HandleAccessRequest(resp, r); ar != nil {
|
||||
ar.Authorized = true
|
||||
server.FinishAccessRequest(resp, r, ar)
|
||||
}
|
||||
if resp.IsError && resp.InternalError != nil {
|
||||
fmt.Printf("ERROR: %s\n", resp.InternalError)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// Information endpoint
|
||||
http.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := server.NewResponse()
|
||||
defer resp.Close()
|
||||
|
||||
if ir := server.HandleInfoRequest(resp, r); ir != nil {
|
||||
server.FinishInfoRequest(resp, r, ir)
|
||||
}
|
||||
osin.OutputJSON(resp, w, r)
|
||||
})
|
||||
|
||||
// Application home endpoint
|
||||
http.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("<html><body>"))
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"/authorize?response_type=code&client_id=1234&state=xyz&scope=everything&redirect_uri=%s\">Login</a><br/>", url.QueryEscape("http://localhost:14000/appauth/code"))))
|
||||
w.Write([]byte("</body></html>"))
|
||||
})
|
||||
|
||||
// Application destination - CODE
|
||||
http.HandleFunc("/appauth/code", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
code := r.Form.Get("code")
|
||||
|
||||
w.Write([]byte("<html><body>"))
|
||||
w.Write([]byte("APP AUTH - CODE<br/>"))
|
||||
defer w.Write([]byte("</body></html>"))
|
||||
|
||||
if code == "" {
|
||||
w.Write([]byte("Nothing to do"))
|
||||
return
|
||||
}
|
||||
|
||||
jr := make(map[string]interface{})
|
||||
|
||||
// build access code url
|
||||
aurl := fmt.Sprintf("/token?grant_type=authorization_code&client_id=1234&client_secret=aabbccdd&state=xyz&redirect_uri=%s&code=%s",
|
||||
url.QueryEscape("http://localhost:14000/appauth/code"), url.QueryEscape(code))
|
||||
|
||||
// if parse, download and parse json
|
||||
if r.Form.Get("doparse") == "1" {
|
||||
err := example.DownloadAccessToken(fmt.Sprintf("http://localhost:14000%s", aurl),
|
||||
&osin.BasicAuth{"1234", "aabbccdd"}, jr)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
w.Write([]byte("<br/>"))
|
||||
}
|
||||
}
|
||||
|
||||
// show json error
|
||||
if erd, ok := jr["error"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ERROR: %s<br/>\n", erd)))
|
||||
}
|
||||
|
||||
// show json access token
|
||||
if at, ok := jr["access_token"]; ok {
|
||||
w.Write([]byte(fmt.Sprintf("ACCESS TOKEN: %s<br/>\n", at)))
|
||||
}
|
||||
|
||||
w.Write([]byte(fmt.Sprintf("FULL RESULT: %+v<br/>\n", jr)))
|
||||
|
||||
// output links
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Goto Token URL</a><br/>", aurl)))
|
||||
|
||||
cururl := *r.URL
|
||||
curq := cururl.Query()
|
||||
curq.Add("doparse", "1")
|
||||
cururl.RawQuery = curq.Encode()
|
||||
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Download Token</a><br/>", cururl.String())))
|
||||
})
|
||||
|
||||
http.ListenAndServe(":14000", nil)
|
||||
}
|
108
vendor/github.com/RangelReale/osin/example/teststorage.go
generated
vendored
Normal file
108
vendor/github.com/RangelReale/osin/example/teststorage.go
generated
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/RangelReale/osin"
|
||||
)
|
||||
|
||||
type TestStorage struct {
|
||||
clients map[string]osin.Client
|
||||
authorize map[string]*osin.AuthorizeData
|
||||
access map[string]*osin.AccessData
|
||||
refresh map[string]string
|
||||
}
|
||||
|
||||
func NewTestStorage() *TestStorage {
|
||||
r := &TestStorage{
|
||||
clients: make(map[string]osin.Client),
|
||||
authorize: make(map[string]*osin.AuthorizeData),
|
||||
access: make(map[string]*osin.AccessData),
|
||||
refresh: make(map[string]string),
|
||||
}
|
||||
|
||||
r.clients["1234"] = &osin.DefaultClient{
|
||||
Id: "1234",
|
||||
Secret: "aabbccdd",
|
||||
RedirectUri: "http://localhost:14000/appauth",
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (s *TestStorage) Clone() osin.Storage {
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *TestStorage) Close() {
|
||||
}
|
||||
|
||||
func (s *TestStorage) GetClient(id string) (osin.Client, error) {
|
||||
fmt.Printf("GetClient: %s\n", id)
|
||||
if c, ok := s.clients[id]; ok {
|
||||
return c, nil
|
||||
}
|
||||
return nil, osin.ErrNotFound
|
||||
}
|
||||
|
||||
func (s *TestStorage) SetClient(id string, client osin.Client) error {
|
||||
fmt.Printf("SetClient: %s\n", id)
|
||||
s.clients[id] = client
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *TestStorage) SaveAuthorize(data *osin.AuthorizeData) error {
|
||||
fmt.Printf("SaveAuthorize: %s\n", data.Code)
|
||||
s.authorize[data.Code] = data
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *TestStorage) LoadAuthorize(code string) (*osin.AuthorizeData, error) {
|
||||
fmt.Printf("LoadAuthorize: %s\n", code)
|
||||
if d, ok := s.authorize[code]; ok {
|
||||
return d, nil
|
||||
}
|
||||
return nil, osin.ErrNotFound
|
||||
}
|
||||
|
||||
func (s *TestStorage) RemoveAuthorize(code string) error {
|
||||
fmt.Printf("RemoveAuthorize: %s\n", code)
|
||||
delete(s.authorize, code)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *TestStorage) SaveAccess(data *osin.AccessData) error {
|
||||
fmt.Printf("SaveAccess: %s\n", data.AccessToken)
|
||||
s.access[data.AccessToken] = data
|
||||
if data.RefreshToken != "" {
|
||||
s.refresh[data.RefreshToken] = data.AccessToken
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *TestStorage) LoadAccess(code string) (*osin.AccessData, error) {
|
||||
fmt.Printf("LoadAccess: %s\n", code)
|
||||
if d, ok := s.access[code]; ok {
|
||||
return d, nil
|
||||
}
|
||||
return nil, osin.ErrNotFound
|
||||
}
|
||||
|
||||
func (s *TestStorage) RemoveAccess(code string) error {
|
||||
fmt.Printf("RemoveAccess: %s\n", code)
|
||||
delete(s.access, code)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *TestStorage) LoadRefresh(code string) (*osin.AccessData, error) {
|
||||
fmt.Printf("LoadRefresh: %s\n", code)
|
||||
if d, ok := s.refresh[code]; ok {
|
||||
return s.LoadAccess(d)
|
||||
}
|
||||
return nil, osin.ErrNotFound
|
||||
}
|
||||
|
||||
func (s *TestStorage) RemoveRefresh(code string) error {
|
||||
fmt.Printf("RemoveRefresh: %s\n", code)
|
||||
delete(s.refresh, code)
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user