Skip to content

Commit

Permalink
Update OSIN interface to latest
Browse files Browse the repository at this point in the history
See RangelReale/osin#21 for details
  • Loading branch information
rmg committed Aug 15, 2014
1 parent 1c94a09 commit f24abfc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
33 changes: 17 additions & 16 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,13 @@ func createOauthClient(w http.ResponseWriter, r *http.Request) {
u5Secret, err := uuid.NewV4()
secret := base64.StdEncoding.EncodeToString([]byte(u5Secret.String()))

newClient := osin.Client{}
newClient.Id = cleanSting
newClient.RedirectUri = newOauthClient.ClientRedirectURI
newClient.Secret = secret
newClient := osin.DefaultClient{
Id: cleanSting,
RedirectUri: newOauthClient.ClientRedirectURI,
Secret: secret,
}

storageID := createOauthClientStorageID(newOauthClient.APIID, newClient.Id)
storageID := createOauthClientStorageID(newOauthClient.APIID, newClient.GetId())
storeErr := genericOsinStorage.SetClient(storageID, &newClient, true)

if storeErr != nil {
Expand All @@ -370,9 +371,9 @@ func createOauthClient(w http.ResponseWriter, r *http.Request) {
}

reportableClientData := OAuthClient{
ClientID: newClient.Id,
ClientSecret: newClient.Secret,
ClientRedirectURI: newClient.RedirectUri,
ClientID: newClient.GetId(),
ClientSecret: newClient.GetSecret(),
ClientRedirectURI: newClient.GetRedirectUri(),
}

responseMessage, err = json.Marshal(&reportableClientData)
Expand All @@ -384,7 +385,7 @@ func createOauthClient(w http.ResponseWriter, r *http.Request) {
code = 500
} else {
log.WithFields(logrus.Fields{
"key": newClient.Id,
"key": newClient.GetId(),
}).Info("New OAuth Client registered successfully.")
}

Expand Down Expand Up @@ -456,9 +457,9 @@ func getOauthClientDetails(keyName string, APIID string) ([]byte, int) {
success = false
} else {
reportableClientData := OAuthClient{
ClientID: thisClientData.Id,
ClientSecret: thisClientData.Secret,
ClientRedirectURI: thisClientData.RedirectUri,
ClientID: thisClientData.GetId(),
ClientSecret: thisClientData.GetSecret(),
ClientRedirectURI: thisClientData.GetRedirectUri(),
}
responseMessage, err = json.Marshal(&reportableClientData)
if err != nil {
Expand Down Expand Up @@ -530,11 +531,11 @@ func getOauthClients(APIID string) ([]byte, int) {
success = false
} else {
clients := []OAuthClient{}
for _, osinClient := range *thisClientData {
for _, osinClient := range thisClientData {
reportableClientData := OAuthClient{
ClientID: osinClient.Id,
ClientSecret: osinClient.Secret,
ClientRedirectURI: osinClient.RedirectUri,
ClientID: osinClient.GetId(),
ClientSecret: osinClient.GetSecret(),
ClientRedirectURI: osinClient.GetRedirectUri(),
}
clients = append(clients, reportableClientData)
}
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ func addOAuthHandlers(spec APISpec, Muxer *http.ServeMux, test bool) {

if test {
log.Warning("Adding test client")
testClient := &osin.Client{
testClient := osin.DefaultClient{
Id: "1234",
Secret: "aabbccdd",
RedirectUri: "http://client.oauth.com",
}
osinStorage.SetClient(testClient.Id, testClient, false)
osinStorage.SetClient(testClient.Id, &testClient, false)
log.Warning("Test client added")
}
osinServer := osin.NewServer(serverConfig, osinStorage)
Expand Down
30 changes: 18 additions & 12 deletions oauth_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,14 @@ type RedisOsinStorageInterface struct {
store StorageHandler
}

func (r RedisOsinStorageInterface) Clone() osin.Storage {
return r
}

func (r RedisOsinStorageInterface) Close() {}

// GetClient will retrieve client data
func (r RedisOsinStorageInterface) GetClient(id string) (*osin.Client, error) {
func (r RedisOsinStorageInterface) GetClient(id string) (osin.Client, error) {
key := CLIENT_PREFIX + id

clientJSON, storeErr := r.store.GetKey(key)
Expand All @@ -287,18 +293,18 @@ func (r RedisOsinStorageInterface) GetClient(id string) (*osin.Client, error) {
return nil, storeErr
}

thisClient := osin.Client{}
thisClient := new(osin.DefaultClient)
if marshalErr := json.Unmarshal([]byte(clientJSON), &thisClient); marshalErr != nil {
log.Error("Couldn't unmarshal OAuth client object")
log.Error(marshalErr)
}

return &thisClient, nil
return thisClient, nil
}

// GetClientNoPrefix will retrieve client data, but not asign a prefix - this is an unfortunate hack,
// but we don't want to change the signature in Osin for GetClient to support the odd Redis prefixing
func (r RedisOsinStorageInterface) GetClientNoPrefix(id string) (*osin.Client, error) {
func (r RedisOsinStorageInterface) GetClientNoPrefix(id string) (osin.Client, error) {

key := id

Expand All @@ -310,17 +316,17 @@ func (r RedisOsinStorageInterface) GetClientNoPrefix(id string) (*osin.Client, e
return nil, storeErr
}

thisClient := osin.Client{}
thisClient := new(osin.DefaultClient)
if marshalErr := json.Unmarshal([]byte(clientJSON), &thisClient); marshalErr != nil {
log.Error("Couldn't unmarshal OAuth client object")
log.Error(marshalErr)
}

return &thisClient, nil
return thisClient, nil
}

// GetClients will retreive a list of clients for a prefix
func (r RedisOsinStorageInterface) GetClients(filter string, ignorePrefix bool) (*[]osin.Client, error) {
func (r RedisOsinStorageInterface) GetClients(filter string, ignorePrefix bool) ([]osin.Client, error) {
key := CLIENT_PREFIX + filter
if ignorePrefix {
key = filter
Expand All @@ -331,20 +337,20 @@ func (r RedisOsinStorageInterface) GetClients(filter string, ignorePrefix bool)
theseClients := []osin.Client{}

for _, clientJSON := range clientJSON {
thisClient := osin.Client{}
thisClient := new(osin.DefaultClient)
if marshalErr := json.Unmarshal([]byte(clientJSON), &thisClient); marshalErr != nil {
log.Error("Couldn't unmarshal OAuth client object")
log.Error(marshalErr)
return &theseClients, marshalErr
return theseClients, marshalErr
}
theseClients = append(theseClients, thisClient)
}

return &theseClients, nil
return theseClients, nil
}

// SetClient creates client data
func (r RedisOsinStorageInterface) SetClient(id string, client *osin.Client, ignorePrefix bool) error {
func (r RedisOsinStorageInterface) SetClient(id string, client osin.Client, ignorePrefix bool) error {
clientDataJSON, err := json.Marshal(client)

if err != nil {
Expand Down Expand Up @@ -438,7 +444,7 @@ func (r RedisOsinStorageInterface) SaveAccess(accessData *osin.AccessData) error
}

// Set the client ID for analytics
newSession.OauthClientID = accessData.Client.Id
newSession.OauthClientID = accessData.Client.GetId()

// Override timeouts so that we can be in sync with Osin
newSession.Expires = time.Now().Unix() + int64(accessData.ExpiresIn)
Expand Down

0 comments on commit f24abfc

Please sign in to comment.