Initial QSfera import
This commit is contained in:
+109
@@ -0,0 +1,109 @@
|
||||
// Copyright 2018-2021 CERN
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// In applying this license, CERN does not waive the privileges and immunities
|
||||
// granted to it by virtue of its status as an Intergovernmental Organization
|
||||
// or submit itself to any jurisdiction.
|
||||
|
||||
package ctx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
ua "github.com/mileusna/useragent"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
// UserAgentHeader is the header used for the user agent
|
||||
const (
|
||||
UserAgentHeader = "x-user-agent"
|
||||
|
||||
WebUserAgent = "web"
|
||||
GrpcUserAgent = "grpc"
|
||||
MobileUserAgent = "mobile"
|
||||
DesktopUserAgent = "desktop"
|
||||
)
|
||||
|
||||
// ContextGetUserAgent returns the user agent if set in the given context.
|
||||
// see https://github.com/grpc/grpc-go/issues/1100
|
||||
func ContextGetUserAgent(ctx context.Context) (*ua.UserAgent, bool) {
|
||||
if userAgentStr, ok := ContextGetUserAgentString(ctx); ok {
|
||||
userAgent := ua.Parse(userAgentStr)
|
||||
return &userAgent, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// ContextGetUserAgentString returns the user agent string if set in the given context.
|
||||
func ContextGetUserAgentString(ctx context.Context) (string, bool) {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
userAgentLst, ok := md[UserAgentHeader]
|
||||
if !ok {
|
||||
userAgentLst, ok = md["user-agent"]
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
if len(userAgentLst) == 0 {
|
||||
return "", false
|
||||
}
|
||||
return userAgentLst[0], true
|
||||
}
|
||||
|
||||
// ContextGetUserAgentCategory returns the category of the user agent
|
||||
// (i.e. if it is a web, mobile, desktop or grpc user agent)
|
||||
func ContextGetUserAgentCategory(ctx context.Context) (string, bool) {
|
||||
agent, ok := ContextGetUserAgent(ctx)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
switch {
|
||||
case isWeb(agent):
|
||||
return WebUserAgent, true
|
||||
case isMobile(agent):
|
||||
return MobileUserAgent, true
|
||||
case isDesktop(agent):
|
||||
return DesktopUserAgent, true
|
||||
case isGRPC(agent):
|
||||
return GrpcUserAgent, true
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
func isWeb(ua *ua.UserAgent) bool {
|
||||
return ua.IsChrome() || ua.IsEdge() || ua.IsFirefox() || ua.IsSafari() ||
|
||||
ua.IsInternetExplorer() || ua.IsOpera() || ua.IsOperaMini()
|
||||
}
|
||||
|
||||
// isMobile returns true if the useragent is generated by the mobile
|
||||
func isMobile(ua *ua.UserAgent) bool {
|
||||
// workaround as the library does not recognise iOS string inside the user agent
|
||||
isIOS := ua.IsIOS() || strings.Contains(ua.String, "iOS")
|
||||
return !isWeb(ua) && (ua.IsAndroid() || isIOS)
|
||||
}
|
||||
|
||||
// isDesktop returns true if the useragent is generated by a desktop application
|
||||
func isDesktop(ua *ua.UserAgent) bool {
|
||||
return ua.Desktop && !isWeb(ua)
|
||||
}
|
||||
|
||||
// isGRPC returns true if the useragent is generated by a grpc client
|
||||
func isGRPC(ua *ua.UserAgent) bool {
|
||||
return strings.HasPrefix(ua.Name, "grpc")
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
// Copyright 2018-2021 CERN
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// In applying this license, CERN does not waive the privileges and immunities
|
||||
// granted to it by virtue of its status as an Intergovernmental Organization
|
||||
// or submit itself to any jurisdiction.
|
||||
|
||||
package ctx
|
||||
|
||||
import "context"
|
||||
|
||||
// InitiatorHeader is the header key used to pass the initiator id to http services and grpc calls.
|
||||
var InitiatorHeader = "initiator-id"
|
||||
|
||||
// ContextGetInitiator returns the initiator if set in the given context.
|
||||
func ContextGetInitiator(ctx context.Context) (string, bool) {
|
||||
i, ok := ctx.Value(initiatorKey).(string)
|
||||
return i, ok
|
||||
}
|
||||
|
||||
// ContextSetInitiator stores the initiator in the context.
|
||||
func ContextSetInitiator(ctx context.Context, i string) context.Context {
|
||||
return context.WithValue(ctx, initiatorKey, i)
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
// Copyright 2018-2021 CERN
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// In applying this license, CERN does not waive the privileges and immunities
|
||||
// granted to it by virtue of its status as an Intergovernmental Organization
|
||||
// or submit itself to any jurisdiction.
|
||||
|
||||
package ctx
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// ContextGetLockID returns the lock id if set in the given context.
|
||||
func ContextGetLockID(ctx context.Context) (string, bool) {
|
||||
u, ok := ctx.Value(lockIDKey).(string)
|
||||
return u, ok
|
||||
}
|
||||
|
||||
// ContextSetLockID stores the lock id in the context.
|
||||
func ContextSetLockID(ctx context.Context, t string) context.Context {
|
||||
return context.WithValue(ctx, lockIDKey, t)
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
// Copyright 2018-2021 CERN
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// In applying this license, CERN does not waive the privileges and immunities
|
||||
// granted to it by virtue of its status as an Intergovernmental Organization
|
||||
// or submit itself to any jurisdiction.
|
||||
|
||||
package ctx
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
auth "github.com/cs3org/go-cs3apis/cs3/auth/provider/v1beta1"
|
||||
)
|
||||
|
||||
// ContextGetScopes returns the scopes if set in the given context.
|
||||
func ContextGetScopes(ctx context.Context) (map[string]*auth.Scope, bool) {
|
||||
s, ok := ctx.Value(scopeKey).(map[string]*auth.Scope)
|
||||
return s, ok
|
||||
}
|
||||
|
||||
// ContextSetScopes stores the scopes in the context.
|
||||
func ContextSetScopes(ctx context.Context, s map[string]*auth.Scope) context.Context {
|
||||
return context.WithValue(ctx, scopeKey, s)
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
// Copyright 2018-2021 CERN
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// In applying this license, CERN does not waive the privileges and immunities
|
||||
// granted to it by virtue of its status as an Intergovernmental Organization
|
||||
// or submit itself to any jurisdiction.
|
||||
|
||||
package ctx
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// TokenHeader is the header to be used across grpc and http services
|
||||
// to forward the access token.
|
||||
const TokenHeader = "x-access-token"
|
||||
|
||||
// ContextGetToken returns the token if set in the given context.
|
||||
func ContextGetToken(ctx context.Context) (string, bool) {
|
||||
u, ok := ctx.Value(tokenKey).(string)
|
||||
return u, ok
|
||||
}
|
||||
|
||||
// ContextMustGetToken panics if token is not in context.
|
||||
func ContextMustGetToken(ctx context.Context) string {
|
||||
u, ok := ContextGetToken(ctx)
|
||||
if !ok {
|
||||
panic("token not found in context")
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
// ContextSetToken stores the token in the context.
|
||||
func ContextSetToken(ctx context.Context, t string) context.Context {
|
||||
return context.WithValue(ctx, tokenKey, t)
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
// Copyright 2018-2021 CERN
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// In applying this license, CERN does not waive the privileges and immunities
|
||||
// granted to it by virtue of its status as an Intergovernmental Organization
|
||||
// or submit itself to any jurisdiction.
|
||||
|
||||
package ctx
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
|
||||
)
|
||||
|
||||
type key int
|
||||
|
||||
const (
|
||||
userKey key = iota
|
||||
tokenKey
|
||||
idKey
|
||||
lockIDKey
|
||||
scopeKey
|
||||
initiatorKey
|
||||
)
|
||||
|
||||
// ContextGetUser returns the user if set in the given context.
|
||||
func ContextGetUser(ctx context.Context) (*userpb.User, bool) {
|
||||
u, ok := ctx.Value(userKey).(*userpb.User)
|
||||
return u, ok
|
||||
}
|
||||
|
||||
// ContextMustGetUser panics if user is not in context.
|
||||
func ContextMustGetUser(ctx context.Context) *userpb.User {
|
||||
u, ok := ContextGetUser(ctx)
|
||||
if !ok {
|
||||
panic("user not found in context")
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
// ContextSetUser stores the user in the context.
|
||||
func ContextSetUser(ctx context.Context, u *userpb.User) context.Context {
|
||||
return context.WithValue(ctx, userKey, u)
|
||||
}
|
||||
|
||||
// ContextGetUserID returns the user if set in the given context.
|
||||
func ContextGetUserID(ctx context.Context) (*userpb.UserId, bool) {
|
||||
u, ok := ctx.Value(idKey).(*userpb.UserId)
|
||||
return u, ok
|
||||
}
|
||||
|
||||
// ContextSetUserID stores the userid in the context.
|
||||
func ContextSetUserID(ctx context.Context, id *userpb.UserId) context.Context {
|
||||
return context.WithValue(ctx, idKey, id)
|
||||
}
|
||||
Reference in New Issue
Block a user