2020-04-21 15:00:32 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2020-06-15 14:50:09 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2020-04-21 15:00:32 +00:00
|
|
|
"github.com/caos/logging"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
2020-06-10 08:47:50 +00:00
|
|
|
"github.com/caos/zitadel/internal/model"
|
2020-04-21 15:00:32 +00:00
|
|
|
proj_model "github.com/caos/zitadel/internal/project/model"
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
2020-06-15 14:50:09 +00:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
2020-04-21 15:00:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func appFromModel(app *proj_model.Application) *Application {
|
|
|
|
creationDate, err := ptypes.TimestampProto(app.CreationDate)
|
|
|
|
logging.Log("GRPC-iejs3").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
|
|
|
changeDate, err := ptypes.TimestampProto(app.ChangeDate)
|
|
|
|
logging.Log("GRPC-di7rw").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
|
|
|
return &Application{
|
|
|
|
Id: app.AppID,
|
|
|
|
State: appStateFromModel(app.State),
|
|
|
|
CreationDate: creationDate,
|
|
|
|
ChangeDate: changeDate,
|
|
|
|
Name: app.Name,
|
|
|
|
Sequence: app.Sequence,
|
|
|
|
AppConfig: appConfigFromModel(app),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func appConfigFromModel(app *proj_model.Application) isApplication_AppConfig {
|
2020-06-23 12:47:47 +00:00
|
|
|
if app.Type == proj_model.AppTypeOIDC {
|
2020-04-21 15:00:32 +00:00
|
|
|
return &Application_OidcConfig{
|
|
|
|
OidcConfig: oidcConfigFromModel(app.OIDCConfig),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func oidcConfigFromModel(config *proj_model.OIDCConfig) *OIDCConfig {
|
|
|
|
return &OIDCConfig{
|
|
|
|
RedirectUris: config.RedirectUris,
|
|
|
|
ResponseTypes: oidcResponseTypesFromModel(config.ResponseTypes),
|
|
|
|
GrantTypes: oidcGrantTypesFromModel(config.GrantTypes),
|
|
|
|
ApplicationType: oidcApplicationTypeFromModel(config.ApplicationType),
|
|
|
|
ClientId: config.ClientID,
|
|
|
|
ClientSecret: config.ClientSecretString,
|
|
|
|
AuthMethodType: oidcAuthMethodTypeFromModel(config.AuthMethodType),
|
|
|
|
PostLogoutRedirectUris: config.PostLogoutRedirectUris,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
func oidcConfigFromApplicationViewModel(app *proj_model.ApplicationView) *OIDCConfig {
|
|
|
|
return &OIDCConfig{
|
|
|
|
RedirectUris: app.OIDCRedirectUris,
|
|
|
|
ResponseTypes: oidcResponseTypesFromModel(app.OIDCResponseTypes),
|
|
|
|
GrantTypes: oidcGrantTypesFromModel(app.OIDCGrantTypes),
|
|
|
|
ApplicationType: oidcApplicationTypeFromModel(app.OIDCApplicationType),
|
|
|
|
ClientId: app.OIDCClientID,
|
|
|
|
AuthMethodType: oidcAuthMethodTypeFromModel(app.OIDCAuthMethodType),
|
|
|
|
PostLogoutRedirectUris: app.OIDCPostLogoutRedirectUris,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 15:00:32 +00:00
|
|
|
func oidcAppCreateToModel(app *OIDCApplicationCreate) *proj_model.Application {
|
|
|
|
return &proj_model.Application{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: app.ProjectId,
|
|
|
|
},
|
|
|
|
Name: app.Name,
|
2020-06-23 12:47:47 +00:00
|
|
|
Type: proj_model.AppTypeOIDC,
|
2020-04-21 15:00:32 +00:00
|
|
|
OIDCConfig: &proj_model.OIDCConfig{
|
|
|
|
RedirectUris: app.RedirectUris,
|
|
|
|
ResponseTypes: oidcResponseTypesToModel(app.ResponseTypes),
|
|
|
|
GrantTypes: oidcGrantTypesToModel(app.GrantTypes),
|
|
|
|
ApplicationType: oidcApplicationTypeToModel(app.ApplicationType),
|
|
|
|
AuthMethodType: oidcAuthMethodTypeToModel(app.AuthMethodType),
|
|
|
|
PostLogoutRedirectUris: app.PostLogoutRedirectUris,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func appUpdateToModel(app *ApplicationUpdate) *proj_model.Application {
|
|
|
|
return &proj_model.Application{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: app.ProjectId,
|
|
|
|
},
|
|
|
|
AppID: app.Id,
|
|
|
|
Name: app.Name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func oidcConfigUpdateToModel(app *OIDCConfigUpdate) *proj_model.OIDCConfig {
|
|
|
|
return &proj_model.OIDCConfig{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: app.ProjectId,
|
|
|
|
},
|
|
|
|
AppID: app.ApplicationId,
|
|
|
|
RedirectUris: app.RedirectUris,
|
|
|
|
ResponseTypes: oidcResponseTypesToModel(app.ResponseTypes),
|
|
|
|
GrantTypes: oidcGrantTypesToModel(app.GrantTypes),
|
|
|
|
ApplicationType: oidcApplicationTypeToModel(app.ApplicationType),
|
|
|
|
AuthMethodType: oidcAuthMethodTypeToModel(app.AuthMethodType),
|
|
|
|
PostLogoutRedirectUris: app.PostLogoutRedirectUris,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
func applicationSearchRequestsToModel(request *ApplicationSearchRequest) *proj_model.ApplicationSearchRequest {
|
|
|
|
return &proj_model.ApplicationSearchRequest{
|
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
2020-06-10 08:47:50 +00:00
|
|
|
Queries: applicationSearchQueriesToModel(request.ProjectId, request.Queries),
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 08:47:50 +00:00
|
|
|
func applicationSearchQueriesToModel(projectID string, queries []*ApplicationSearchQuery) []*proj_model.ApplicationSearchQuery {
|
|
|
|
converted := make([]*proj_model.ApplicationSearchQuery, len(queries)+1)
|
2020-05-11 10:16:29 +00:00
|
|
|
for i, q := range queries {
|
|
|
|
converted[i] = applicationSearchQueryToModel(q)
|
|
|
|
}
|
2020-06-23 12:47:47 +00:00
|
|
|
converted[len(queries)] = &proj_model.ApplicationSearchQuery{Key: proj_model.AppSearchKeyProjectID, Method: model.SearchMethodEquals, Value: projectID}
|
2020-06-10 08:47:50 +00:00
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
func applicationSearchQueryToModel(query *ApplicationSearchQuery) *proj_model.ApplicationSearchQuery {
|
|
|
|
return &proj_model.ApplicationSearchQuery{
|
|
|
|
Key: applicationSearchKeyToModel(query.Key),
|
|
|
|
Method: searchMethodToModel(query.Method),
|
|
|
|
Value: query.Value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 12:47:47 +00:00
|
|
|
func applicationSearchKeyToModel(key ApplicationSearchKey) proj_model.AppSearchKey {
|
2020-05-11 10:16:29 +00:00
|
|
|
switch key {
|
|
|
|
case ApplicationSearchKey_APPLICATIONSEARCHKEY_APP_NAME:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.AppSearchKeyName
|
2020-05-11 10:16:29 +00:00
|
|
|
default:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.AppSearchKeyUnspecified
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func applicationSearchResponseFromModel(response *proj_model.ApplicationSearchResponse) *ApplicationSearchResponse {
|
|
|
|
return &ApplicationSearchResponse{
|
|
|
|
Offset: response.Offset,
|
|
|
|
Limit: response.Limit,
|
|
|
|
TotalResult: response.TotalResult,
|
|
|
|
Result: applicationViewsFromModel(response.Result),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func applicationViewsFromModel(apps []*proj_model.ApplicationView) []*ApplicationView {
|
|
|
|
converted := make([]*ApplicationView, len(apps))
|
|
|
|
for i, app := range apps {
|
|
|
|
converted[i] = applicationViewFromModel(app)
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
func applicationViewFromModel(application *proj_model.ApplicationView) *ApplicationView {
|
|
|
|
creationDate, err := ptypes.TimestampProto(application.CreationDate)
|
|
|
|
logging.Log("GRPC-lo9sw").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
|
|
|
changeDate, err := ptypes.TimestampProto(application.ChangeDate)
|
|
|
|
logging.Log("GRPC-8uwsd").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
|
|
|
converted := &ApplicationView{
|
|
|
|
Id: application.ID,
|
|
|
|
State: appStateFromModel(application.State),
|
|
|
|
CreationDate: creationDate,
|
|
|
|
ChangeDate: changeDate,
|
|
|
|
Name: application.Name,
|
|
|
|
Sequence: application.Sequence,
|
|
|
|
}
|
|
|
|
if application.IsOIDC {
|
|
|
|
converted.AppConfig = &ApplicationView_OidcConfig{
|
|
|
|
OidcConfig: oidcConfigFromApplicationViewModel(application),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2020-04-21 15:00:32 +00:00
|
|
|
func appStateFromModel(state proj_model.AppState) AppState {
|
|
|
|
switch state {
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.AppStateActive:
|
2020-04-21 15:00:32 +00:00
|
|
|
return AppState_APPSTATE_ACTIVE
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.AppStateInactive:
|
2020-04-21 15:00:32 +00:00
|
|
|
return AppState_APPSTATE_INACTIVE
|
|
|
|
default:
|
|
|
|
return AppState_APPSTATE_UNSPECIFIED
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func oidcResponseTypesToModel(responseTypes []OIDCResponseType) []proj_model.OIDCResponseType {
|
|
|
|
if responseTypes == nil || len(responseTypes) == 0 {
|
2020-06-23 12:47:47 +00:00
|
|
|
return []proj_model.OIDCResponseType{proj_model.OIDCResponseTypeCode}
|
2020-04-21 15:00:32 +00:00
|
|
|
}
|
|
|
|
oidcResponseTypes := make([]proj_model.OIDCResponseType, len(responseTypes))
|
|
|
|
|
|
|
|
for i, responseType := range responseTypes {
|
|
|
|
switch responseType {
|
|
|
|
case OIDCResponseType_OIDCRESPONSETYPE_CODE:
|
2020-06-23 12:47:47 +00:00
|
|
|
oidcResponseTypes[i] = proj_model.OIDCResponseTypeCode
|
2020-04-21 15:00:32 +00:00
|
|
|
case OIDCResponseType_OIDCRESPONSETYPE_ID_TOKEN:
|
2020-06-23 12:47:47 +00:00
|
|
|
oidcResponseTypes[i] = proj_model.OIDCResponseTypeIDToken
|
2020-04-21 15:00:32 +00:00
|
|
|
case OIDCResponseType_OIDCRESPONSETYPE_TOKEN:
|
2020-06-23 12:47:47 +00:00
|
|
|
oidcResponseTypes[i] = proj_model.OIDCResponseTypeToken
|
2020-04-21 15:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return oidcResponseTypes
|
|
|
|
}
|
|
|
|
|
|
|
|
func oidcResponseTypesFromModel(responseTypes []proj_model.OIDCResponseType) []OIDCResponseType {
|
|
|
|
oidcResponseTypes := make([]OIDCResponseType, len(responseTypes))
|
|
|
|
|
|
|
|
for i, responseType := range responseTypes {
|
|
|
|
switch responseType {
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.OIDCResponseTypeCode:
|
2020-04-21 15:00:32 +00:00
|
|
|
oidcResponseTypes[i] = OIDCResponseType_OIDCRESPONSETYPE_CODE
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.OIDCResponseTypeIDToken:
|
2020-04-21 15:00:32 +00:00
|
|
|
oidcResponseTypes[i] = OIDCResponseType_OIDCRESPONSETYPE_ID_TOKEN
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.OIDCResponseTypeToken:
|
2020-04-21 15:00:32 +00:00
|
|
|
oidcResponseTypes[i] = OIDCResponseType_OIDCRESPONSETYPE_TOKEN
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return oidcResponseTypes
|
|
|
|
}
|
|
|
|
|
|
|
|
func oidcGrantTypesToModel(grantTypes []OIDCGrantType) []proj_model.OIDCGrantType {
|
|
|
|
if grantTypes == nil || len(grantTypes) == 0 {
|
2020-06-23 12:47:47 +00:00
|
|
|
return []proj_model.OIDCGrantType{proj_model.OIDCGrantTypeAuthorizationCode}
|
2020-04-21 15:00:32 +00:00
|
|
|
}
|
|
|
|
oidcGrantTypes := make([]proj_model.OIDCGrantType, len(grantTypes))
|
|
|
|
|
|
|
|
for i, grantType := range grantTypes {
|
|
|
|
switch grantType {
|
|
|
|
case OIDCGrantType_OIDCGRANTTYPE_AUTHORIZATION_CODE:
|
2020-06-23 12:47:47 +00:00
|
|
|
oidcGrantTypes[i] = proj_model.OIDCGrantTypeAuthorizationCode
|
2020-04-21 15:00:32 +00:00
|
|
|
case OIDCGrantType_OIDCGRANTTYPE_IMPLICIT:
|
2020-06-23 12:47:47 +00:00
|
|
|
oidcGrantTypes[i] = proj_model.OIDCGrantTypeImplicit
|
2020-04-21 15:00:32 +00:00
|
|
|
case OIDCGrantType_OIDCGRANTTYPE_REFRESH_TOKEN:
|
2020-06-23 12:47:47 +00:00
|
|
|
oidcGrantTypes[i] = proj_model.OIDCGrantTypeRefreshToken
|
2020-04-21 15:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return oidcGrantTypes
|
|
|
|
}
|
|
|
|
|
|
|
|
func oidcGrantTypesFromModel(grantTypes []proj_model.OIDCGrantType) []OIDCGrantType {
|
|
|
|
oidcGrantTypes := make([]OIDCGrantType, len(grantTypes))
|
|
|
|
|
|
|
|
for i, grantType := range grantTypes {
|
|
|
|
switch grantType {
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.OIDCGrantTypeAuthorizationCode:
|
2020-04-21 15:00:32 +00:00
|
|
|
oidcGrantTypes[i] = OIDCGrantType_OIDCGRANTTYPE_AUTHORIZATION_CODE
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.OIDCGrantTypeImplicit:
|
2020-04-21 15:00:32 +00:00
|
|
|
oidcGrantTypes[i] = OIDCGrantType_OIDCGRANTTYPE_IMPLICIT
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.OIDCGrantTypeRefreshToken:
|
2020-04-21 15:00:32 +00:00
|
|
|
oidcGrantTypes[i] = OIDCGrantType_OIDCGRANTTYPE_REFRESH_TOKEN
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return oidcGrantTypes
|
|
|
|
}
|
|
|
|
|
|
|
|
func oidcApplicationTypeToModel(appType OIDCApplicationType) proj_model.OIDCApplicationType {
|
|
|
|
switch appType {
|
|
|
|
case OIDCApplicationType_OIDCAPPLICATIONTYPE_WEB:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.OIDCApplicationTypeWeb
|
2020-04-21 15:00:32 +00:00
|
|
|
case OIDCApplicationType_OIDCAPPLICATIONTYPE_USER_AGENT:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.OIDCApplicationTypeUserAgent
|
2020-04-21 15:00:32 +00:00
|
|
|
case OIDCApplicationType_OIDCAPPLICATIONTYPE_NATIVE:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.OIDCApplicationTypeNative
|
2020-04-21 15:00:32 +00:00
|
|
|
}
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.OIDCApplicationTypeWeb
|
2020-04-21 15:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func oidcApplicationTypeFromModel(appType proj_model.OIDCApplicationType) OIDCApplicationType {
|
|
|
|
switch appType {
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.OIDCApplicationTypeWeb:
|
2020-04-21 15:00:32 +00:00
|
|
|
return OIDCApplicationType_OIDCAPPLICATIONTYPE_WEB
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.OIDCApplicationTypeUserAgent:
|
2020-04-21 15:00:32 +00:00
|
|
|
return OIDCApplicationType_OIDCAPPLICATIONTYPE_USER_AGENT
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.OIDCApplicationTypeNative:
|
2020-04-21 15:00:32 +00:00
|
|
|
return OIDCApplicationType_OIDCAPPLICATIONTYPE_NATIVE
|
|
|
|
default:
|
|
|
|
return OIDCApplicationType_OIDCAPPLICATIONTYPE_WEB
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func oidcAuthMethodTypeToModel(authType OIDCAuthMethodType) proj_model.OIDCAuthMethodType {
|
|
|
|
switch authType {
|
|
|
|
case OIDCAuthMethodType_OIDCAUTHMETHODTYPE_BASIC:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.OIDCAuthMethodTypeBasic
|
2020-04-21 15:00:32 +00:00
|
|
|
case OIDCAuthMethodType_OIDCAUTHMETHODTYPE_POST:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.OIDCAuthMethodTypePost
|
2020-04-21 15:00:32 +00:00
|
|
|
case OIDCAuthMethodType_OIDCAUTHMETHODTYPE_NONE:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.OIDCAuthMethodTypeNone
|
2020-04-21 15:00:32 +00:00
|
|
|
default:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.OIDCAuthMethodTypeBasic
|
2020-04-21 15:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func oidcAuthMethodTypeFromModel(authType proj_model.OIDCAuthMethodType) OIDCAuthMethodType {
|
|
|
|
switch authType {
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.OIDCAuthMethodTypeBasic:
|
2020-04-21 15:00:32 +00:00
|
|
|
return OIDCAuthMethodType_OIDCAUTHMETHODTYPE_BASIC
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.OIDCAuthMethodTypePost:
|
2020-04-21 15:00:32 +00:00
|
|
|
return OIDCAuthMethodType_OIDCAUTHMETHODTYPE_POST
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.OIDCAuthMethodTypeNone:
|
2020-04-21 15:00:32 +00:00
|
|
|
return OIDCAuthMethodType_OIDCAUTHMETHODTYPE_NONE
|
|
|
|
default:
|
|
|
|
return OIDCAuthMethodType_OIDCAUTHMETHODTYPE_BASIC
|
|
|
|
}
|
|
|
|
}
|
2020-06-15 14:50:09 +00:00
|
|
|
|
|
|
|
func appChangesToResponse(response *proj_model.ApplicationChanges, offset uint64, limit uint64) (_ *Changes) {
|
|
|
|
return &Changes{
|
|
|
|
Limit: limit,
|
|
|
|
Offset: offset,
|
|
|
|
Changes: appChangesToMgtAPI(response),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func appChangesToMgtAPI(changes *proj_model.ApplicationChanges) (_ []*Change) {
|
|
|
|
result := make([]*Change, len(changes.Changes))
|
|
|
|
|
|
|
|
for i, change := range changes.Changes {
|
|
|
|
b, err := json.Marshal(change.Data)
|
|
|
|
data := &structpb.Struct{}
|
|
|
|
err = protojson.Unmarshal(b, data)
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
result[i] = &Change{
|
|
|
|
ChangeDate: change.ChangeDate,
|
|
|
|
EventType: change.EventType,
|
|
|
|
Sequence: change.Sequence,
|
2020-06-25 09:25:38 +00:00
|
|
|
Editor: change.Modifier,
|
2020-06-15 14:50:09 +00:00
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|