Giter VIP home page Giter VIP logo

fosite-example's Introduction

ORY Fosite Example Server

Build Status

ORY Fosite is the security first OAuth2 & OpenID Connect framework for Go. Built simple, powerful and extensible. This repository contains an exemplary http server using ORY Fosite for serving OAuth2 requests.

Install and run

The Fosite example server requires [email protected] or higher installed as it uses go modules for dependency management. Once installed, run the demo:

$ go get -d github.com/ory/fosite-example
$ cd $GOPATH/src/github.com/ory/fosite-example
$ go run main.go

fosite-example's People

Contributors

aeneasr avatar arekkas avatar deltamualpha avatar dependabot[bot] avatar matthewhartstonge avatar michaelboke avatar mitar avatar pmundt avatar rplnt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fosite-example's Issues

error on go install

github.com/ory/fosite-example/vendor/github.com/ory/fosite/compose
vendor/github.com/ory/fosite/compose/compose_openid.go:28: cannot use "github.com/ory-am/fosite/handler/oauth2".AuthorizeImplicitGrantTypeHandler literal (type *"github.com/ory-am/fosite/handler/oauth2".AuthorizeImplicitGrantTypeHandler) as type *"github.com/ory/fosite/handler/oauth2".AuthorizeImplicitGrantTypeHandler in field value
vendor/github.com/ory/fosite/compose/compose_openid.go:47: cannot use "github.com/ory-am/fosite/handler/oauth2".AuthorizeExplicitGrantHandler literal (type *"github.com/ory-am/fosite/handler/oauth2".AuthorizeExplicitGrantHandler) as type *"github.com/ory/fosite/handler/oauth2".AuthorizeExplicitGrantHandler in field value
vendor/github.com/ory/fosite/compose/compose_openid.go:53: cannot use "github.com/ory-am/fosite/handler/oauth2".AuthorizeImplicitGrantTypeHandler literal (type *"github.com/ory-am/fosite/handler/oauth2".AuthorizeImplicitGrantTypeHandler) as type *"github.com/ory/fosite/handler/oauth2".AuthorizeImplicitGrantTypeHandler in field value
vendor/github.com/ory/fosite/compose/compose_strategy.go:21: cannot use "github.com/ory-am/fosite/token/hmac".HMACStrategy literal (type *"github.com/ory-am/fosite/token/hmac".HMACStrategy) as type *"github.com/ory/fosite/token/hmac".HMACStrategy in field value
vendor/github.com/ory/fosite/compose/compose_strategy.go:31: cannot use "github.com/ory-am/fosite/token/jwt".RS256JWTStrategy literal (type *"github.com/ory-am/fosite/token/jwt".RS256JWTStrategy) as type *"github.com/ory/fosite/token/jwt".RS256JWTStrategy in field value
vendor/github.com/ory/fosite/compose/compose_strategy.go:39: cannot use "github.com/ory-am/fosite/token/jwt".RS256JWTStrategy literal (type *"github.com/ory-am/fosite/token/jwt".RS256JWTStrategy) as type *"github.com/ory/fosite/token/jwt".RS256JWTStrategy in field value

github.com/ory/fosite-example/vendor/github.com/ory/fosite/handler/openid

vendor/github.com/ory/fosite/handler/openid/flow_hybrid.go:58: cannot use ar (type "github.com/ory-am/fosite".AuthorizeRequester) as type "github.com/ory/fosite".Requester in argument to c.AuthorizeExplicitGrantHandler.AuthorizeCodeStrategy.GenerateAuthorizeCode:
"github.com/ory-am/fosite".AuthorizeRequester does not implement "github.com/ory/fosite".Requester (wrong type for GetClient method)
have GetClient() "github.com/ory-am/fosite".Client
want GetClient() "github.com/ory/fosite".Client
vendor/github.com/ory/fosite/handler/openid/flow_hybrid.go:61: cannot use ar (type "github.com/ory-am/fosite".AuthorizeRequester) as type "github.com/ory/fosite".Requester in argument to c.AuthorizeExplicitGrantHandler.AuthorizeCodeGrantStorage.CreateAuthorizeCodeSession:
"github.com/ory-am/fosite".AuthorizeRequester does not implement "github.com/ory/fosite".Requester (wrong type for GetClient method)
have GetClient() "github.com/ory-am/fosite".Client
want GetClient() "github.com/ory/fosite".Client
vendor/github.com/ory/fosite/handler/openid/flow_hybrid.go:72: cannot use ([]byte)(base64.URLEncoding.EncodeToString(([]byte)(hash[:c.Enigma.GetSigningMethodLength() / 2]))) (type []byte) as type string in assignment
vendor/github.com/ory/fosite/handler/openid/flow_hybrid.go:78: too many arguments in call to c.AuthorizeImplicitGrantTypeHandler.IssueImplicitAccessToken
have ("github.com/ory/fosite-example/vendor/golang.org/x/net/context".Context, *http.Request, "github.com/ory-am/fosite".AuthorizeRequester, "github.com/ory-am/fosite".AuthorizeResponder)
want ("context".Context, "github.com/ory/fosite".AuthorizeRequester, "github.com/ory/fosite".AuthorizeResponder)
vendor/github.com/ory/fosite/handler/openid/flow_hybrid.go:87: cannot use ([]byte)(base64.URLEncoding.EncodeToString(([]byte)(hash[:c.Enigma.GetSigningMethodLength() / 2]))) (type []byte) as type string in assignment
vendor/github.com/ory/fosite/handler/openid/flow_implicit.go:56: too many arguments in call to c.AuthorizeImplicitGrantTypeHandler.IssueImplicitAccessToken
have ("github.com/ory/fosite-example/vendor/golang.org/x/net/context".Context, *http.Request, "github.com/ory-am/fosite".AuthorizeRequester, "github.com/ory-am/fosite".AuthorizeResponder)
want ("context".Context, "github.com/ory/fosite".AuthorizeRequester, "github.com/ory/fosite".AuthorizeResponder)
vendor/github.com/ory/fosite/handler/openid/flow_implicit.go:66: cannot use ([]byte)(base64.URLEncoding.EncodeToString(([]byte)(hash[:c.RS256JWTStrategy.GetSigningMethodLength() / 2]))) (type []byte) as type string in assignment

Multiple problems in installing and running the example [Solutions to all provided in this issue]

I was trying to run your example and ran into multiple problems. I fixed all those and was finally able to run the example on port 3846 as intended.

I have described my process stepwise below. There are two approaches, the second one solved the problem, the first one will bring to your notice an issue which I could not fix.

I could not send a pull request with my solution because the solution requires changes inside files in vendor directory. But this is generated by the glide install command. This is the reason why I tried first approach, but it brings to our notice a compatibility issue between latest commit of fosite and fosite-example.

Please have a look.

Approach 1:

  1. go get github.com/Masterminds/glide

  2. go get -d github.com/ory/fosite

  3. C:\Users\Aman Kapoor>go get github.com/ory/fosite-example
    github.com/ory/fosite-example/authorizationserver
    C:\Gospace\src\github.com\ory\fosite-example\authorizationserver\oauth2.go:61: cannot use compose.OAuth2AuthorizeExplicitFactory (type func(*compose.Config, interface {}, interface {}) interface {}) as type fosite.Hasher in argument to compose.Compose:
    func(*compose.Config, interface {}, interface {}) interface {} does not implement fosite.Hasher (missing Compare method)

  4. So, I did go get -d -u github.com/ory/fosite-example and it worked. I did it because you suggested it in #5 (issue 5)

  5. Next step according to you is to change the directory to fosite-example. Did that.

  6. And then glide install

C:\Gospace\src\github.com\ory\fosite-example>glide install
[WARN] Lock file may be out of date. Hash check of YAML failed. You may need to run 'update'
[INFO] Downloading dependencies. Please wait...
[INFO] --> Fetching github.com/square/go-jose
[INFO] --> Fetching github.com/asaskevich/govalidator
[INFO] --> Fetching github.com/moul/http2curl
[INFO] --> Fetching github.com/ory/fosite
[INFO] --> Fetching github.com/golang/protobuf
[INFO] --> Fetching github.com/parnurzeal/gorequest
[INFO] --> Fetching github.com/pborman/uuid
[INFO] --> Fetching github.com/pkg/errors
[INFO] --> Fetching github.com/dgrijalva/jwt-go
[INFO] --> Fetching google.golang.org/appengine
[INFO] --> Fetching golang.org/x/crypto
[INFO] --> Fetching golang.org/x/net
[INFO] --> Fetching golang.org/x/oauth2
[INFO] Setting references.
[INFO] --> Setting version for github.com/dgrijalva/jwt-go to d2709f9f1f31ebcda9651b03077758c1f3a0018c.
[INFO] --> Setting version for github.com/parnurzeal/gorequest to 2aea80ce763523ecc6452e61c3727ae9595a5
809.
[INFO] --> Setting version for github.com/golang/protobuf to c3cefd437628a0b7d31b34fe44b3a7a540e98527.
[INFO] --> Setting version for github.com/asaskevich/govalidator to 7b3beb6df3c42abd3509abfc3bcacc0fbfb
7c877.
[INFO] --> Setting version for github.com/moul/http2curl to b1479103caacaa39319f75e7f57fc545287fca0d.
[INFO] --> Setting version for github.com/square/go-jose to aa2e30fdd1fe9dd3394119af66451ae790d50e0d.
[INFO] --> Setting version for github.com/pkg/errors to 645ef00459ed84a119197bfb8d8205042c6df63d.
[INFO] --> Setting version for github.com/pborman/uuid to a97ce2ca70fa5a848076093f05e639a89ca34d06.
[INFO] --> Setting version for github.com/ory/fosite to eb9077f6608d776ae50eb2ad4205705bad6ee0eb.
[INFO] --> Setting version for google.golang.org/appengine to b4728023490a62e70ba739ff62aa65ffcca84210.

[INFO] --> Setting version for golang.org/x/crypto to e0d166c33c321d0ff863f459a5882096e334f508.
[INFO] --> Setting version for golang.org/x/net to 075e191f18186a8ff2becaf64478e30f4545cdad.
[INFO] --> Setting version for golang.org/x/oauth2 to 04e1573abc896e70388bd387a69753c378d46466.
[INFO] Exporting resolved dependencies...
[INFO] --> Exporting github.com/asaskevich/govalidator
[INFO] --> Exporting github.com/golang/protobuf
[INFO] --> Exporting github.com/moul/http2curl
[INFO] --> Exporting github.com/ory/fosite
[INFO] --> Exporting github.com/parnurzeal/gorequest
[INFO] --> Exporting github.com/dgrijalva/jwt-go
[INFO] --> Exporting github.com/pborman/uuid
[INFO] --> Exporting github.com/pkg/errors
[INFO] --> Exporting github.com/square/go-jose
[INFO] --> Exporting google.golang.org/appengine
[INFO] --> Exporting golang.org/x/crypto
[INFO] --> Exporting golang.org/x/net
[INFO] --> Exporting golang.org/x/oauth2
[INFO] Replacing existing vendor dependencies

  1. C:\Gospace\src\github.com\ory\fosite-example>go install

vendor\github.com\ory\fosite\compose\compose.go:6:2: cannot find package "github.com/ory-am/fosite" in any of:
C:\Gospace\src\github.com\ory\fosite-example\vendor\github.com\ory-am\fosite (vendor tree)
C:\Go\src\github.com\ory-am\fosite (from $GOROOT)
C:\Gospace\src\github.com\ory-am\fosite (from $GOPATH)
vendor\github.com\ory\fosite\compose\compose_oauth2.go:5:2: cannot find package "github.com/ory-am/fosite/handler/oauth2" in any of:
C:\Gospace\src\github.com\ory\fosite-example\vendor\github.com\ory-am\fosite\handler\oauth2 (vendor tree)
C:\Go\src\github.com\ory-am\fosite\handler\oauth2 (from $GOROOT)
C:\Gospace\src\github.com\ory-am\fosite\handler\oauth2 (from $GOPATH)
vendor\github.com\ory\fosite\compose\compose_openid.go:6:2: cannot find package "github.com/ory-am/fosite/handler/openid" in any of:
C:\Gospace\src\github.com\ory\fosite-example\vendor\github.com\ory-am\fosite\handler\openid (vendor tree)
C:\Go\src\github.com\ory-am\fosite\handler\openid (from $GOROOT)
C:\Gospace\src\github.com\ory-am\fosite\handler\openid (from $GOPATH)
vendor\github.com\ory\fosite\compose\compose_strategy.go:8:2: cannot find package "github.com/ory-am/fosite/token/hmac" in any of:
C:\Gospace\src\github.com\ory\fosite-example\vendor\github.com\ory-am\fosite\token\hmac (vendor tree)
C:\Go\src\github.com\ory-am\fosite\token\hmac (from $GOROOT)
C:\Gospace\src\github.com\ory-am\fosite\token\hmac (from $GOPATH)
vendor\github.com\ory\fosite\compose\compose_strategy.go:9:2: cannot find package "github.com/ory-am/fosite/token/jwt" in any of:
C:\Gospace\src\github.com\ory\fosite-example\vendor\github.com\ory-am\fosite\token\jwt (vendor tree)
C:\Go\src\github.com\ory-am\fosite\token\jwt (from $GOROOT)
C:\Gospace\src\github.com\ory-am\fosite\token\jwt (from $GOPATH)

This problem is because of change from ory-am to ory and glide installs this version of your repo ory/fosite@eb9077f

  1. I changed glide.yaml file and glide.lock file to have 0.8.0 and 349726028d42f3c60aeefc67aef06f9f907ccf94 (your latest commit when writing this issue)

  2. And then I do glide install.

  3. Then I do go install, which gives this
    C:\Gospace\src\github.com\ory\fosite-example>go install

github.com/ory/fosite-example/authorizationserver
authorizationserver\oauth2.go:61: cannot use compose.OAuth2AuthorizeExplicitFactory (type func(*compose.
Config, interface {}, interface {}) interface {}) as type fosite.Hasher in argument to compose.Compose:
func(*compose.Config, interface {}, interface {}) interface {} does not implement fosite.Hasher
(missing Compare method)

This error is same as in step 3.


Approach 2 which worked.

Step 1: Do step 6 of approach 1.

Step 2: Find and replace ory-am with ory in all the files under fosite-example folder. (Most of these changes will be done under vendor\github.com\ory\fosite; around 109 files are there)

Step 3: go install
C:\Gospace\src\github.com\ory\fosite-example>go install
github.com/ory/fosite-example/authorizationserver
authorizationserver\oauth2_auth.go:81: not enough arguments in call to oauth2.NewAuthorizeResponse
have ("github.com/ory/fosite-example/vendor/golang.org/x/net/context".Context, fosite.AuthorizeRequester, *openid.DefaultSession)
want ("github.com/ory/fosite-example/vendor/golang.org/x/net/context".Context, *http.Request, fosite.AuthorizeRequester, fosite.Session)
authorizationserver\oauth2_token.go:40: not enough arguments in call to oauth2.NewAccessResponse
have ("github.com/ory/fosite-example/vendor/golang.org/x/net/context".Context, fosite.AccessRequester)
want ("github.com/ory/fosite-example/vendor/golang.org/x/net/context".Context, *http.Request, fosite.AccessRequester)

Step 4: To fix first error in Step 3, I go to this piece of code authorizationserver\oauth2_auth.go:81: and change response, err := oauth2.NewAuthorizeResponse(ctx, ar, mySessionData) to response, err := oauth2.NewAuthorizeResponse(ctx, req, ar, mySessionData)

Step 5: To fix second error in Step 3, I go to this piece of code authorizationserver\oauth2_token.go:40: and change response, err := oauth2.NewAccessResponse(ctx, accessRequest) to response, err := oauth2.NewAccessResponse(ctx, req, accessRequest)

Step 4: go install and it works.

PS: In my first time, when I downloaded this example, I fixed it using second approach only. But then I thought why should we find and replace, and instead I imported the latest commit of fosite, and this way the first approach and its reccuring error came into existence. Your latest fosite code is not working with fosite-example, and I could not fix it. I hope you make necessary changes in latest code to make it work.

I am learning to integrate it in my site that I am building.

Revoke token returning invalid_request

In the example after executing the exchange step I get invalid_request when trying to revoke the access or refresh token.

Payload

It's a public client so no secret is passed in the header

Header:

authHash := base64.StdEncoding.EncodeToString(
    []byte(fmt.Sprintf("%s:%s", oAuth2Config.ClientID, "")),
)
r.Header.Set("Authorization", "Basic "+authHash)
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")

Body:

token=valid-existing-non-revoked-access-token-or-refresh-token
token_type_hint=tried-with-access-and-refresh-tokens-same-result

Response

{"error":"invalid_request","error_description":"The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed","error_hint":"Make sure that the various parameters are correct, be aware of case sensitivity and trim your parameters. Make sure that the client you are using has exactly whitelisted the redirect_uri you specified.","status_code":400}

Can't debug fosite itself but confirmed that the request coming as param to NewRevocationRequest contains the token and token_type_hint. I assume it can't find the proper loader here but not sure.

Here's the stack trace

Error: invalid_request
github.com/ory/fosite.(*Fosite).NewRevocationRequest
        /Users/eze/Workspace/pkg/mod/github.com/ory/fosite@v0.32.1/revoke_handler.go:79
github.com/ory/fosite-example/authorizationserver.revokeEndpoint
        /Users/eze/Workspace/src/github.com/ory/fosite-example/authorizationserver/oauth2_revoke.go:13
net/http.HandlerFunc.ServeHTTP
        /usr/local/Cellar/go/1.14.6/libexec/src/net/http/server.go:2041
net/http.(*ServeMux).ServeHTTP
        /usr/local/Cellar/go/1.14.6/libexec/src/net/http/server.go:2416
net/http.serverHandler.ServeHTTP
        /usr/local/Cellar/go/1.14.6/libexec/src/net/http/server.go:2836
net/http.(*conn).serve
        /usr/local/Cellar/go/1.14.6/libexec/src/net/http/server.go:1924
runtime.goexit
        /usr/local/Cellar/go/1.14.6/libexec/src/runtime/asm_amd64.s:1373

Up to date

Hi guys!
Could you update the example to the latest version of fosite?

Currently, we have v0.29.6, when this example is using v0.20.3
and project is not buildable because of api changes

How to use JWT as access token?

Hi, I've tried this but got the error. Can anyone tell me what am I missing here?

NOTE : I only changed this portion of the code.

// in file authorizationserver/oauth2.go

var oauth2 = ComposeJWTAccessToken(config, store, privateKey)

func ComposeJWTAccessToken(config *fosite.Config, storage interface{}, key interface{}) fosite.OAuth2Provider {
	keyGetter := func(context.Context) interface{} {
		return key
	}
	return compose.Compose(
		config,
		storage,
		&compose.CommonStrategy{
			CoreStrategy: compose.NewOAuth2JWTStrategy(
				func(ctx context.Context) interface{} { return privateKey2 },
				compose.NewOAuth2HMACStrategy(config), config),
			OpenIDConnectTokenStrategy: compose.NewOpenIDConnectStrategy(keyGetter, config),
			Signer:                     &jwt.DefaultSigner{GetPrivateKey: keyGetter},
		},
		compose.OAuth2AuthorizeExplicitFactory,
		compose.OAuth2AuthorizeImplicitFactory,
		compose.OAuth2ClientCredentialsGrantFactory,
		compose.OAuth2RefreshTokenGrantFactory,
		compose.OAuth2ResourceOwnerPasswordCredentialsFactory,
		compose.RFC7523AssertionGrantFactory,

		compose.OpenIDConnectExplicitFactory,
		compose.OpenIDConnectImplicitFactory,
		compose.OpenIDConnectHybridFactory,
		compose.OpenIDConnectRefreshFactory,

		compose.OAuth2TokenIntrospectionFactory,
		compose.OAuth2TokenRevocationFactory,

		compose.OAuth2PKCEFactory,
	)
}

Got this error:

I tried to exchange the authorize code for an access token but it did not work 
but got error: oauth2: cannot fetch token: 400 Bad Request 
Response: 
{
    "error":"invalid_grant",
    "error_description":"The provided authorization grant (e.g., authorization code, resource owner credentials) 
or refresh token is invalid, expired, revoked, 
does not match the redirection URI used in the authorization request,
 or was issued to another client. Unable to find initial PKCE data tied to this request"
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.