-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Christofas
committed
Oct 1, 2021
1 parent
990abd6
commit 50cdb03
Showing
3 changed files
with
80 additions
and
12 deletions.
There are no files selected for viewing
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,6 @@ | ||
Enhancement: Add a middleware to authenticate public share requests | ||
|
||
Added a new middleware to authenticate public share requests. This makes it possible to use APIs which require an authenticated context with public shares. | ||
|
||
https://github.com/owncloud/ocis/pull/2536 | ||
https://github.com/owncloud/ocis/issues/2479 |
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,58 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" | ||
) | ||
|
||
const ( | ||
headerRevaAccessToken = "x-access-token" | ||
headerShareToken = "public-token" | ||
appProviderPathPrefix = "/app/open" | ||
basicAuthPasswordPrefix = "basic|" | ||
authenticationType = "publicshares" | ||
) | ||
|
||
// PublicShareAuth ... | ||
func PublicShareAuth(opts ...Option) func(next http.Handler) http.Handler { | ||
options := newOptions(opts...) | ||
logger := options.Logger | ||
|
||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Currently we only want to authenticate app open request coming from public shares. | ||
shareToken := r.Header.Get(headerShareToken) | ||
if shareToken == "" || !strings.HasPrefix(appProviderPathPrefix, r.URL.Path) { | ||
// Don't authenticate | ||
next.ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
// We can ignore the username since it is always set to "public" in public shares. | ||
_, password, ok := r.BasicAuth() | ||
|
||
sharePassword := basicAuthPasswordPrefix | ||
if ok { | ||
sharePassword += password | ||
} | ||
|
||
authResp, err := options.RevaGatewayClient.Authenticate(r.Context(), &gateway.AuthenticateRequest{ | ||
Type: authenticationType, | ||
ClientId: shareToken, | ||
ClientSecret: sharePassword, | ||
}) | ||
|
||
if err != nil { | ||
logger.Debug().Err(err).Str("public_share_token", shareToken).Msg("could not authenticate public share") | ||
// try another middleware | ||
next.ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
r.Header.Add(headerRevaAccessToken, authResp.Token) | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
} |