From f956c182a6633793d3cb8d00c37e4daceb8799b2 Mon Sep 17 00:00:00 2001 From: ssenerg Date: Mon, 2 Nov 2026 10:31:42 +0330 Subject: [PATCH] Ask before handing an app its sign-in code A link anyone sends can begin an app's sign-in with their own PKCE challenge, and another app on the person's device may claim the apps' scheme, so the browser no longer goes straight back to the app with a code. The server's page names the GoChat account and what the code does (sign in, link, or sign in again to delete), warns against links someone sent, and gives the code to the app only when the person taps Continue. FinishSSO says what an app's code does, and for whom, with app_code_use, app_username and email. --- README.md | 15 +- api/protobuf/gen/gochat/v1/auth.pb.go | 336 +++++++++++------- api/protobuf/proto/gochat/v1/auth.proto | 17 + internal/server/drpc/sso.go | 1 + internal/services/accounts/delete_test.go | 3 +- internal/services/accounts/messages.go | 6 + internal/services/accounts/sso.go | 35 +- internal/services/accounts/sso_test.go | 7 +- internal/web/exits_test.go | 2 +- internal/web/sso.go | 29 +- internal/web/sso_flow_test.go | 19 +- .../web/views/templates/pages/sso-app.html | 19 + mobile/README.md | 5 +- 13 files changed, 346 insertions(+), 148 deletions(-) create mode 100644 internal/web/views/templates/pages/sso-app.html diff --git a/README.md b/README.md index 8743976..725deb3 100644 --- a/README.md +++ b/README.md @@ -176,8 +176,9 @@ it does for the web: Apps sign in with `AuthService/Login` (and `VerifyMFA` for two-factor accounts) and send the session token as `Authorization: Bearer `. With single sign-on they list the providers with `ListSSOProviders`, open -`/login/sso/?app=` in the system browser, which -comes back to `io.github.ssenerg.gochat:/sso?code=` (or `?error=`), and +`/login/sso/?app=` in the system browser, which, +once the person confirms on the server's page, comes back to +`io.github.ssenerg.gochat:/sso?code=` (or straight to `?error=`), and trade the code and their PKCE verifier for a session with `RedeemSSO`. Signed in, they link another provider account the same way: `BeginSSOLink` gives a ticket for `&link=`, and `LinkSSO` takes the code with the @@ -415,7 +416,15 @@ that carry what people post. `ASWebAuthenticationSession`). The browser comes back to the app's scheme with a code that lasts two minutes, works once, and only with the PKCE verifier the app began with, so another app claiming the - scheme gets nothing of use. + scheme gets nothing of use from a flow the app began. + - Anyone can send a link that begins an app's flow with their own + challenge, and another app on the person's device may claim the + scheme, so the server never hands a code over by itself. Its page + names the GoChat account and what the code does (sign in, link, or + sign in again to delete) and warns against links someone sent; the + code goes to the app only when the person taps Continue. Claimed + HTTPS redirects would close this for good, but each server's domain + would have to be built into the apps. - An app links an account at a provider from that browser too, though the browser has no session. `BeginSSOLink`, called with the app's session, gives a ticket naming the person and the app's PKCE diff --git a/api/protobuf/gen/gochat/v1/auth.pb.go b/api/protobuf/gen/gochat/v1/auth.pb.go index 22410a9..becdffe 100644 --- a/api/protobuf/gen/gochat/v1/auth.pb.go +++ b/api/protobuf/gen/gochat/v1/auth.pb.go @@ -21,6 +21,62 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) +// What an app's code does. +type AppCodeUse int32 + +const ( + AppCodeUse_APP_CODE_USE_UNSPECIFIED AppCodeUse = 0 + // Signs the app in. + AppCodeUse_APP_CODE_USE_SIGN_IN AppCodeUse = 1 + // Links the account at the provider to the app's. + AppCodeUse_APP_CODE_USE_LINK AppCodeUse = 2 + // Proves to the app that its account's owner signed in again. + AppCodeUse_APP_CODE_USE_REAUTH AppCodeUse = 3 +) + +// Enum value maps for AppCodeUse. +var ( + AppCodeUse_name = map[int32]string{ + 0: "APP_CODE_USE_UNSPECIFIED", + 1: "APP_CODE_USE_SIGN_IN", + 2: "APP_CODE_USE_LINK", + 3: "APP_CODE_USE_REAUTH", + } + AppCodeUse_value = map[string]int32{ + "APP_CODE_USE_UNSPECIFIED": 0, + "APP_CODE_USE_SIGN_IN": 1, + "APP_CODE_USE_LINK": 2, + "APP_CODE_USE_REAUTH": 3, + } +) + +func (x AppCodeUse) Enum() *AppCodeUse { + p := new(AppCodeUse) + *p = x + return p +} + +func (x AppCodeUse) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (AppCodeUse) Descriptor() protoreflect.EnumDescriptor { + return file_gochat_v1_auth_proto_enumTypes[0].Descriptor() +} + +func (AppCodeUse) Type() protoreflect.EnumType { + return &file_gochat_v1_auth_proto_enumTypes[0] +} + +func (x AppCodeUse) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use AppCodeUse.Descriptor instead. +func (AppCodeUse) EnumDescriptor() ([]byte, []int) { + return file_gochat_v1_auth_proto_rawDescGZIP(), []int{0} +} + type GetSignupInfoRequest struct { state protoimpl.MessageState `protogen:"open.v1"` InviteToken string `protobuf:"bytes,1,opt,name=invite_token,json=inviteToken,proto3" json:"invite_token,omitempty"` @@ -1720,7 +1776,13 @@ type FinishSSOResponse struct { Result isFinishSSOResponse_Result `protobuf_oneof:"result"` ProviderName string `protobuf:"bytes,6,opt,name=provider_name,json=providerName,proto3" json:"provider_name,omitempty"` // The team an invite joined, for a new account. - Team *Team `protobuf:"bytes,7,opt,name=team,proto3" json:"team,omitempty"` + Team *Team `protobuf:"bytes,7,opt,name=team,proto3" json:"team,omitempty"` + // With an app_code: what it does and the GoChat account it is for, which + // the browser asks the person to confirm before the app gets the code. + AppCodeUse AppCodeUse `protobuf:"varint,9,opt,name=app_code_use,json=appCodeUse,proto3,enum=gochat.v1.AppCodeUse" json:"app_code_use,omitempty"` + AppUsername string `protobuf:"bytes,10,opt,name=app_username,json=appUsername,proto3" json:"app_username,omitempty"` + // The account's email at the provider, when it gives one. + Email string `protobuf:"bytes,11,opt,name=email,proto3" json:"email,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -1830,6 +1892,27 @@ func (x *FinishSSOResponse) GetTeam() *Team { return nil } +func (x *FinishSSOResponse) GetAppCodeUse() AppCodeUse { + if x != nil { + return x.AppCodeUse + } + return AppCodeUse_APP_CODE_USE_UNSPECIFIED +} + +func (x *FinishSSOResponse) GetAppUsername() string { + if x != nil { + return x.AppUsername + } + return "" +} + +func (x *FinishSSOResponse) GetEmail() string { + if x != nil { + return x.Email + } + return "" +} + type isFinishSSOResponse_Result interface { isFinishSSOResponse_Result() } @@ -2602,7 +2685,7 @@ const file_gochat_v1_auth_proto_rawDesc = "" + "\x10FinishSSORequest\x12\x14\n" + "\x05state\x18\x01 \x01(\tR\x05state\x12\x12\n" + "\x04code\x18\x02 \x01(\tR\x04code\x12!\n" + - "\finvite_token\x18\x03 \x01(\tR\vinviteToken\"\xb3\x02\n" + + "\finvite_token\x18\x03 \x01(\tR\vinviteToken\"\xa5\x03\n" + "\x11FinishSSOResponse\x12.\n" + "\asession\x18\x01 \x01(\v2\x12.gochat.v1.SessionH\x00R\asession\x12\x1d\n" + "\tmfa_token\x18\x02 \x01(\tH\x00R\bmfaToken\x12\x1b\n" + @@ -2612,7 +2695,12 @@ const file_gochat_v1_auth_proto_rawDesc = "" + "\x06linked\x18\x05 \x01(\bH\x00R\x06linked\x12#\n" + "\freauth_token\x18\b \x01(\tH\x00R\vreauthToken\x12#\n" + "\rprovider_name\x18\x06 \x01(\tR\fproviderName\x12#\n" + - "\x04team\x18\a \x01(\v2\x0f.gochat.v1.TeamR\x04teamB\b\n" + + "\x04team\x18\a \x01(\v2\x0f.gochat.v1.TeamR\x04team\x127\n" + + "\fapp_code_use\x18\t \x01(\x0e2\x15.gochat.v1.AppCodeUseR\n" + + "appCodeUse\x12!\n" + + "\fapp_username\x18\n" + + " \x01(\tR\vappUsername\x12\x14\n" + + "\x05email\x18\v \x01(\tR\x05emailB\b\n" + "\x06result\"B\n" + "\x10RedeemSSORequest\x12\x12\n" + "\x04code\x18\x01 \x01(\tR\x04code\x12\x1a\n" + @@ -2650,7 +2738,13 @@ const file_gochat_v1_auth_proto_rawDesc = "" + "\auser_id\x18\x01 \x01(\tR\x06userId\x12\x16\n" + "\x06issuer\x18\x02 \x01(\tR\x06issuer\x12\x18\n" + "\asubject\x18\x03 \x01(\tR\asubject\"\x13\n" + - "\x11UnlinkSSOResponse2\xf7\r\n" + + "\x11UnlinkSSOResponse*t\n" + + "\n" + + "AppCodeUse\x12\x1c\n" + + "\x18APP_CODE_USE_UNSPECIFIED\x10\x00\x12\x18\n" + + "\x14APP_CODE_USE_SIGN_IN\x10\x01\x12\x15\n" + + "\x11APP_CODE_USE_LINK\x10\x02\x12\x17\n" + + "\x13APP_CODE_USE_REAUTH\x10\x032\xf7\r\n" + "\vAuthService\x12R\n" + "\rGetSignupInfo\x12\x1f.gochat.v1.GetSignupInfoRequest\x1a .gochat.v1.GetSignupInfoResponse\x12=\n" + "\x06SignUp\x12\x18.gochat.v1.SignUpRequest\x1a\x19.gochat.v1.SignUpResponse\x12:\n" + @@ -2691,124 +2785,127 @@ func file_gochat_v1_auth_proto_rawDescGZIP() []byte { return file_gochat_v1_auth_proto_rawDescData } +var file_gochat_v1_auth_proto_enumTypes = make([]protoimpl.EnumInfo, 1) var file_gochat_v1_auth_proto_msgTypes = make([]protoimpl.MessageInfo, 46) var file_gochat_v1_auth_proto_goTypes = []any{ - (*GetSignupInfoRequest)(nil), // 0: gochat.v1.GetSignupInfoRequest - (*GetSignupInfoResponse)(nil), // 1: gochat.v1.GetSignupInfoResponse - (*SignUpRequest)(nil), // 2: gochat.v1.SignUpRequest - (*SignUpResponse)(nil), // 3: gochat.v1.SignUpResponse - (*LoginRequest)(nil), // 4: gochat.v1.LoginRequest - (*LoginResponse)(nil), // 5: gochat.v1.LoginResponse - (*VerifyMFARequest)(nil), // 6: gochat.v1.VerifyMFARequest - (*VerifyMFAResponse)(nil), // 7: gochat.v1.VerifyMFAResponse - (*LogoutRequest)(nil), // 8: gochat.v1.LogoutRequest - (*LogoutResponse)(nil), // 9: gochat.v1.LogoutResponse - (*MeRequest)(nil), // 10: gochat.v1.MeRequest - (*MeResponse)(nil), // 11: gochat.v1.MeResponse - (*ChangePasswordRequest)(nil), // 12: gochat.v1.ChangePasswordRequest - (*ChangePasswordResponse)(nil), // 13: gochat.v1.ChangePasswordResponse - (*BeginTOTPSetupRequest)(nil), // 14: gochat.v1.BeginTOTPSetupRequest - (*BeginTOTPSetupResponse)(nil), // 15: gochat.v1.BeginTOTPSetupResponse - (*ConfirmTOTPSetupRequest)(nil), // 16: gochat.v1.ConfirmTOTPSetupRequest - (*ConfirmTOTPSetupResponse)(nil), // 17: gochat.v1.ConfirmTOTPSetupResponse - (*DisableTOTPRequest)(nil), // 18: gochat.v1.DisableTOTPRequest - (*DisableTOTPResponse)(nil), // 19: gochat.v1.DisableTOTPResponse - (*DeleteAccountRequest)(nil), // 20: gochat.v1.DeleteAccountRequest - (*DeleteAccountResponse)(nil), // 21: gochat.v1.DeleteAccountResponse - (*ListSessionsRequest)(nil), // 22: gochat.v1.ListSessionsRequest - (*ListSessionsResponse)(nil), // 23: gochat.v1.ListSessionsResponse - (*RevokeSessionRequest)(nil), // 24: gochat.v1.RevokeSessionRequest - (*RevokeSessionResponse)(nil), // 25: gochat.v1.RevokeSessionResponse - (*RevokeSessionsRequest)(nil), // 26: gochat.v1.RevokeSessionsRequest - (*RevokeSessionsResponse)(nil), // 27: gochat.v1.RevokeSessionsResponse - (*ListSSOProvidersRequest)(nil), // 28: gochat.v1.ListSSOProvidersRequest - (*ListSSOProvidersResponse)(nil), // 29: gochat.v1.ListSSOProvidersResponse - (*BeginSSORequest)(nil), // 30: gochat.v1.BeginSSORequest - (*BeginSSOResponse)(nil), // 31: gochat.v1.BeginSSOResponse - (*FinishSSORequest)(nil), // 32: gochat.v1.FinishSSORequest - (*FinishSSOResponse)(nil), // 33: gochat.v1.FinishSSOResponse - (*RedeemSSORequest)(nil), // 34: gochat.v1.RedeemSSORequest - (*RedeemSSOResponse)(nil), // 35: gochat.v1.RedeemSSOResponse - (*BeginSSOLinkRequest)(nil), // 36: gochat.v1.BeginSSOLinkRequest - (*BeginSSOLinkResponse)(nil), // 37: gochat.v1.BeginSSOLinkResponse - (*GetSSOLinkRequest)(nil), // 38: gochat.v1.GetSSOLinkRequest - (*GetSSOLinkResponse)(nil), // 39: gochat.v1.GetSSOLinkResponse - (*LinkSSORequest)(nil), // 40: gochat.v1.LinkSSORequest - (*LinkSSOResponse)(nil), // 41: gochat.v1.LinkSSOResponse - (*ListSSOIdentitiesRequest)(nil), // 42: gochat.v1.ListSSOIdentitiesRequest - (*ListSSOIdentitiesResponse)(nil), // 43: gochat.v1.ListSSOIdentitiesResponse - (*UnlinkSSORequest)(nil), // 44: gochat.v1.UnlinkSSORequest - (*UnlinkSSOResponse)(nil), // 45: gochat.v1.UnlinkSSOResponse - (*Session)(nil), // 46: gochat.v1.Session - (*Team)(nil), // 47: gochat.v1.Team - (*User)(nil), // 48: gochat.v1.User - (*SessionInfo)(nil), // 49: gochat.v1.SessionInfo - (*SSOProvider)(nil), // 50: gochat.v1.SSOProvider - (*SSOIdentity)(nil), // 51: gochat.v1.SSOIdentity + (AppCodeUse)(0), // 0: gochat.v1.AppCodeUse + (*GetSignupInfoRequest)(nil), // 1: gochat.v1.GetSignupInfoRequest + (*GetSignupInfoResponse)(nil), // 2: gochat.v1.GetSignupInfoResponse + (*SignUpRequest)(nil), // 3: gochat.v1.SignUpRequest + (*SignUpResponse)(nil), // 4: gochat.v1.SignUpResponse + (*LoginRequest)(nil), // 5: gochat.v1.LoginRequest + (*LoginResponse)(nil), // 6: gochat.v1.LoginResponse + (*VerifyMFARequest)(nil), // 7: gochat.v1.VerifyMFARequest + (*VerifyMFAResponse)(nil), // 8: gochat.v1.VerifyMFAResponse + (*LogoutRequest)(nil), // 9: gochat.v1.LogoutRequest + (*LogoutResponse)(nil), // 10: gochat.v1.LogoutResponse + (*MeRequest)(nil), // 11: gochat.v1.MeRequest + (*MeResponse)(nil), // 12: gochat.v1.MeResponse + (*ChangePasswordRequest)(nil), // 13: gochat.v1.ChangePasswordRequest + (*ChangePasswordResponse)(nil), // 14: gochat.v1.ChangePasswordResponse + (*BeginTOTPSetupRequest)(nil), // 15: gochat.v1.BeginTOTPSetupRequest + (*BeginTOTPSetupResponse)(nil), // 16: gochat.v1.BeginTOTPSetupResponse + (*ConfirmTOTPSetupRequest)(nil), // 17: gochat.v1.ConfirmTOTPSetupRequest + (*ConfirmTOTPSetupResponse)(nil), // 18: gochat.v1.ConfirmTOTPSetupResponse + (*DisableTOTPRequest)(nil), // 19: gochat.v1.DisableTOTPRequest + (*DisableTOTPResponse)(nil), // 20: gochat.v1.DisableTOTPResponse + (*DeleteAccountRequest)(nil), // 21: gochat.v1.DeleteAccountRequest + (*DeleteAccountResponse)(nil), // 22: gochat.v1.DeleteAccountResponse + (*ListSessionsRequest)(nil), // 23: gochat.v1.ListSessionsRequest + (*ListSessionsResponse)(nil), // 24: gochat.v1.ListSessionsResponse + (*RevokeSessionRequest)(nil), // 25: gochat.v1.RevokeSessionRequest + (*RevokeSessionResponse)(nil), // 26: gochat.v1.RevokeSessionResponse + (*RevokeSessionsRequest)(nil), // 27: gochat.v1.RevokeSessionsRequest + (*RevokeSessionsResponse)(nil), // 28: gochat.v1.RevokeSessionsResponse + (*ListSSOProvidersRequest)(nil), // 29: gochat.v1.ListSSOProvidersRequest + (*ListSSOProvidersResponse)(nil), // 30: gochat.v1.ListSSOProvidersResponse + (*BeginSSORequest)(nil), // 31: gochat.v1.BeginSSORequest + (*BeginSSOResponse)(nil), // 32: gochat.v1.BeginSSOResponse + (*FinishSSORequest)(nil), // 33: gochat.v1.FinishSSORequest + (*FinishSSOResponse)(nil), // 34: gochat.v1.FinishSSOResponse + (*RedeemSSORequest)(nil), // 35: gochat.v1.RedeemSSORequest + (*RedeemSSOResponse)(nil), // 36: gochat.v1.RedeemSSOResponse + (*BeginSSOLinkRequest)(nil), // 37: gochat.v1.BeginSSOLinkRequest + (*BeginSSOLinkResponse)(nil), // 38: gochat.v1.BeginSSOLinkResponse + (*GetSSOLinkRequest)(nil), // 39: gochat.v1.GetSSOLinkRequest + (*GetSSOLinkResponse)(nil), // 40: gochat.v1.GetSSOLinkResponse + (*LinkSSORequest)(nil), // 41: gochat.v1.LinkSSORequest + (*LinkSSOResponse)(nil), // 42: gochat.v1.LinkSSOResponse + (*ListSSOIdentitiesRequest)(nil), // 43: gochat.v1.ListSSOIdentitiesRequest + (*ListSSOIdentitiesResponse)(nil), // 44: gochat.v1.ListSSOIdentitiesResponse + (*UnlinkSSORequest)(nil), // 45: gochat.v1.UnlinkSSORequest + (*UnlinkSSOResponse)(nil), // 46: gochat.v1.UnlinkSSOResponse + (*Session)(nil), // 47: gochat.v1.Session + (*Team)(nil), // 48: gochat.v1.Team + (*User)(nil), // 49: gochat.v1.User + (*SessionInfo)(nil), // 50: gochat.v1.SessionInfo + (*SSOProvider)(nil), // 51: gochat.v1.SSOProvider + (*SSOIdentity)(nil), // 52: gochat.v1.SSOIdentity } var file_gochat_v1_auth_proto_depIdxs = []int32{ - 46, // 0: gochat.v1.SignUpResponse.session:type_name -> gochat.v1.Session - 47, // 1: gochat.v1.SignUpResponse.team:type_name -> gochat.v1.Team - 46, // 2: gochat.v1.LoginResponse.session:type_name -> gochat.v1.Session - 46, // 3: gochat.v1.VerifyMFAResponse.session:type_name -> gochat.v1.Session - 48, // 4: gochat.v1.MeResponse.user:type_name -> gochat.v1.User - 49, // 5: gochat.v1.ListSessionsResponse.sessions:type_name -> gochat.v1.SessionInfo - 50, // 6: gochat.v1.ListSSOProvidersResponse.providers:type_name -> gochat.v1.SSOProvider - 46, // 7: gochat.v1.FinishSSOResponse.session:type_name -> gochat.v1.Session - 47, // 8: gochat.v1.FinishSSOResponse.team:type_name -> gochat.v1.Team - 46, // 9: gochat.v1.RedeemSSOResponse.session:type_name -> gochat.v1.Session - 51, // 10: gochat.v1.ListSSOIdentitiesResponse.identities:type_name -> gochat.v1.SSOIdentity - 0, // 11: gochat.v1.AuthService.GetSignupInfo:input_type -> gochat.v1.GetSignupInfoRequest - 2, // 12: gochat.v1.AuthService.SignUp:input_type -> gochat.v1.SignUpRequest - 4, // 13: gochat.v1.AuthService.Login:input_type -> gochat.v1.LoginRequest - 6, // 14: gochat.v1.AuthService.VerifyMFA:input_type -> gochat.v1.VerifyMFARequest - 8, // 15: gochat.v1.AuthService.Logout:input_type -> gochat.v1.LogoutRequest - 10, // 16: gochat.v1.AuthService.Me:input_type -> gochat.v1.MeRequest - 12, // 17: gochat.v1.AuthService.ChangePassword:input_type -> gochat.v1.ChangePasswordRequest - 14, // 18: gochat.v1.AuthService.BeginTOTPSetup:input_type -> gochat.v1.BeginTOTPSetupRequest - 16, // 19: gochat.v1.AuthService.ConfirmTOTPSetup:input_type -> gochat.v1.ConfirmTOTPSetupRequest - 18, // 20: gochat.v1.AuthService.DisableTOTP:input_type -> gochat.v1.DisableTOTPRequest - 20, // 21: gochat.v1.AuthService.DeleteAccount:input_type -> gochat.v1.DeleteAccountRequest - 22, // 22: gochat.v1.AuthService.ListSessions:input_type -> gochat.v1.ListSessionsRequest - 24, // 23: gochat.v1.AuthService.RevokeSession:input_type -> gochat.v1.RevokeSessionRequest - 26, // 24: gochat.v1.AuthService.RevokeSessions:input_type -> gochat.v1.RevokeSessionsRequest - 28, // 25: gochat.v1.AuthService.ListSSOProviders:input_type -> gochat.v1.ListSSOProvidersRequest - 30, // 26: gochat.v1.AuthService.BeginSSO:input_type -> gochat.v1.BeginSSORequest - 32, // 27: gochat.v1.AuthService.FinishSSO:input_type -> gochat.v1.FinishSSORequest - 34, // 28: gochat.v1.AuthService.RedeemSSO:input_type -> gochat.v1.RedeemSSORequest - 36, // 29: gochat.v1.AuthService.BeginSSOLink:input_type -> gochat.v1.BeginSSOLinkRequest - 38, // 30: gochat.v1.AuthService.GetSSOLink:input_type -> gochat.v1.GetSSOLinkRequest - 40, // 31: gochat.v1.AuthService.LinkSSO:input_type -> gochat.v1.LinkSSORequest - 42, // 32: gochat.v1.AuthService.ListSSOIdentities:input_type -> gochat.v1.ListSSOIdentitiesRequest - 44, // 33: gochat.v1.AuthService.UnlinkSSO:input_type -> gochat.v1.UnlinkSSORequest - 1, // 34: gochat.v1.AuthService.GetSignupInfo:output_type -> gochat.v1.GetSignupInfoResponse - 3, // 35: gochat.v1.AuthService.SignUp:output_type -> gochat.v1.SignUpResponse - 5, // 36: gochat.v1.AuthService.Login:output_type -> gochat.v1.LoginResponse - 7, // 37: gochat.v1.AuthService.VerifyMFA:output_type -> gochat.v1.VerifyMFAResponse - 9, // 38: gochat.v1.AuthService.Logout:output_type -> gochat.v1.LogoutResponse - 11, // 39: gochat.v1.AuthService.Me:output_type -> gochat.v1.MeResponse - 13, // 40: gochat.v1.AuthService.ChangePassword:output_type -> gochat.v1.ChangePasswordResponse - 15, // 41: gochat.v1.AuthService.BeginTOTPSetup:output_type -> gochat.v1.BeginTOTPSetupResponse - 17, // 42: gochat.v1.AuthService.ConfirmTOTPSetup:output_type -> gochat.v1.ConfirmTOTPSetupResponse - 19, // 43: gochat.v1.AuthService.DisableTOTP:output_type -> gochat.v1.DisableTOTPResponse - 21, // 44: gochat.v1.AuthService.DeleteAccount:output_type -> gochat.v1.DeleteAccountResponse - 23, // 45: gochat.v1.AuthService.ListSessions:output_type -> gochat.v1.ListSessionsResponse - 25, // 46: gochat.v1.AuthService.RevokeSession:output_type -> gochat.v1.RevokeSessionResponse - 27, // 47: gochat.v1.AuthService.RevokeSessions:output_type -> gochat.v1.RevokeSessionsResponse - 29, // 48: gochat.v1.AuthService.ListSSOProviders:output_type -> gochat.v1.ListSSOProvidersResponse - 31, // 49: gochat.v1.AuthService.BeginSSO:output_type -> gochat.v1.BeginSSOResponse - 33, // 50: gochat.v1.AuthService.FinishSSO:output_type -> gochat.v1.FinishSSOResponse - 35, // 51: gochat.v1.AuthService.RedeemSSO:output_type -> gochat.v1.RedeemSSOResponse - 37, // 52: gochat.v1.AuthService.BeginSSOLink:output_type -> gochat.v1.BeginSSOLinkResponse - 39, // 53: gochat.v1.AuthService.GetSSOLink:output_type -> gochat.v1.GetSSOLinkResponse - 41, // 54: gochat.v1.AuthService.LinkSSO:output_type -> gochat.v1.LinkSSOResponse - 43, // 55: gochat.v1.AuthService.ListSSOIdentities:output_type -> gochat.v1.ListSSOIdentitiesResponse - 45, // 56: gochat.v1.AuthService.UnlinkSSO:output_type -> gochat.v1.UnlinkSSOResponse - 34, // [34:57] is the sub-list for method output_type - 11, // [11:34] is the sub-list for method input_type - 11, // [11:11] is the sub-list for extension type_name - 11, // [11:11] is the sub-list for extension extendee - 0, // [0:11] is the sub-list for field type_name + 47, // 0: gochat.v1.SignUpResponse.session:type_name -> gochat.v1.Session + 48, // 1: gochat.v1.SignUpResponse.team:type_name -> gochat.v1.Team + 47, // 2: gochat.v1.LoginResponse.session:type_name -> gochat.v1.Session + 47, // 3: gochat.v1.VerifyMFAResponse.session:type_name -> gochat.v1.Session + 49, // 4: gochat.v1.MeResponse.user:type_name -> gochat.v1.User + 50, // 5: gochat.v1.ListSessionsResponse.sessions:type_name -> gochat.v1.SessionInfo + 51, // 6: gochat.v1.ListSSOProvidersResponse.providers:type_name -> gochat.v1.SSOProvider + 47, // 7: gochat.v1.FinishSSOResponse.session:type_name -> gochat.v1.Session + 48, // 8: gochat.v1.FinishSSOResponse.team:type_name -> gochat.v1.Team + 0, // 9: gochat.v1.FinishSSOResponse.app_code_use:type_name -> gochat.v1.AppCodeUse + 47, // 10: gochat.v1.RedeemSSOResponse.session:type_name -> gochat.v1.Session + 52, // 11: gochat.v1.ListSSOIdentitiesResponse.identities:type_name -> gochat.v1.SSOIdentity + 1, // 12: gochat.v1.AuthService.GetSignupInfo:input_type -> gochat.v1.GetSignupInfoRequest + 3, // 13: gochat.v1.AuthService.SignUp:input_type -> gochat.v1.SignUpRequest + 5, // 14: gochat.v1.AuthService.Login:input_type -> gochat.v1.LoginRequest + 7, // 15: gochat.v1.AuthService.VerifyMFA:input_type -> gochat.v1.VerifyMFARequest + 9, // 16: gochat.v1.AuthService.Logout:input_type -> gochat.v1.LogoutRequest + 11, // 17: gochat.v1.AuthService.Me:input_type -> gochat.v1.MeRequest + 13, // 18: gochat.v1.AuthService.ChangePassword:input_type -> gochat.v1.ChangePasswordRequest + 15, // 19: gochat.v1.AuthService.BeginTOTPSetup:input_type -> gochat.v1.BeginTOTPSetupRequest + 17, // 20: gochat.v1.AuthService.ConfirmTOTPSetup:input_type -> gochat.v1.ConfirmTOTPSetupRequest + 19, // 21: gochat.v1.AuthService.DisableTOTP:input_type -> gochat.v1.DisableTOTPRequest + 21, // 22: gochat.v1.AuthService.DeleteAccount:input_type -> gochat.v1.DeleteAccountRequest + 23, // 23: gochat.v1.AuthService.ListSessions:input_type -> gochat.v1.ListSessionsRequest + 25, // 24: gochat.v1.AuthService.RevokeSession:input_type -> gochat.v1.RevokeSessionRequest + 27, // 25: gochat.v1.AuthService.RevokeSessions:input_type -> gochat.v1.RevokeSessionsRequest + 29, // 26: gochat.v1.AuthService.ListSSOProviders:input_type -> gochat.v1.ListSSOProvidersRequest + 31, // 27: gochat.v1.AuthService.BeginSSO:input_type -> gochat.v1.BeginSSORequest + 33, // 28: gochat.v1.AuthService.FinishSSO:input_type -> gochat.v1.FinishSSORequest + 35, // 29: gochat.v1.AuthService.RedeemSSO:input_type -> gochat.v1.RedeemSSORequest + 37, // 30: gochat.v1.AuthService.BeginSSOLink:input_type -> gochat.v1.BeginSSOLinkRequest + 39, // 31: gochat.v1.AuthService.GetSSOLink:input_type -> gochat.v1.GetSSOLinkRequest + 41, // 32: gochat.v1.AuthService.LinkSSO:input_type -> gochat.v1.LinkSSORequest + 43, // 33: gochat.v1.AuthService.ListSSOIdentities:input_type -> gochat.v1.ListSSOIdentitiesRequest + 45, // 34: gochat.v1.AuthService.UnlinkSSO:input_type -> gochat.v1.UnlinkSSORequest + 2, // 35: gochat.v1.AuthService.GetSignupInfo:output_type -> gochat.v1.GetSignupInfoResponse + 4, // 36: gochat.v1.AuthService.SignUp:output_type -> gochat.v1.SignUpResponse + 6, // 37: gochat.v1.AuthService.Login:output_type -> gochat.v1.LoginResponse + 8, // 38: gochat.v1.AuthService.VerifyMFA:output_type -> gochat.v1.VerifyMFAResponse + 10, // 39: gochat.v1.AuthService.Logout:output_type -> gochat.v1.LogoutResponse + 12, // 40: gochat.v1.AuthService.Me:output_type -> gochat.v1.MeResponse + 14, // 41: gochat.v1.AuthService.ChangePassword:output_type -> gochat.v1.ChangePasswordResponse + 16, // 42: gochat.v1.AuthService.BeginTOTPSetup:output_type -> gochat.v1.BeginTOTPSetupResponse + 18, // 43: gochat.v1.AuthService.ConfirmTOTPSetup:output_type -> gochat.v1.ConfirmTOTPSetupResponse + 20, // 44: gochat.v1.AuthService.DisableTOTP:output_type -> gochat.v1.DisableTOTPResponse + 22, // 45: gochat.v1.AuthService.DeleteAccount:output_type -> gochat.v1.DeleteAccountResponse + 24, // 46: gochat.v1.AuthService.ListSessions:output_type -> gochat.v1.ListSessionsResponse + 26, // 47: gochat.v1.AuthService.RevokeSession:output_type -> gochat.v1.RevokeSessionResponse + 28, // 48: gochat.v1.AuthService.RevokeSessions:output_type -> gochat.v1.RevokeSessionsResponse + 30, // 49: gochat.v1.AuthService.ListSSOProviders:output_type -> gochat.v1.ListSSOProvidersResponse + 32, // 50: gochat.v1.AuthService.BeginSSO:output_type -> gochat.v1.BeginSSOResponse + 34, // 51: gochat.v1.AuthService.FinishSSO:output_type -> gochat.v1.FinishSSOResponse + 36, // 52: gochat.v1.AuthService.RedeemSSO:output_type -> gochat.v1.RedeemSSOResponse + 38, // 53: gochat.v1.AuthService.BeginSSOLink:output_type -> gochat.v1.BeginSSOLinkResponse + 40, // 54: gochat.v1.AuthService.GetSSOLink:output_type -> gochat.v1.GetSSOLinkResponse + 42, // 55: gochat.v1.AuthService.LinkSSO:output_type -> gochat.v1.LinkSSOResponse + 44, // 56: gochat.v1.AuthService.ListSSOIdentities:output_type -> gochat.v1.ListSSOIdentitiesResponse + 46, // 57: gochat.v1.AuthService.UnlinkSSO:output_type -> gochat.v1.UnlinkSSOResponse + 35, // [35:58] is the sub-list for method output_type + 12, // [12:35] is the sub-list for method input_type + 12, // [12:12] is the sub-list for extension type_name + 12, // [12:12] is the sub-list for extension extendee + 0, // [0:12] is the sub-list for field type_name } func init() { file_gochat_v1_auth_proto_init() } @@ -2838,13 +2935,14 @@ func file_gochat_v1_auth_proto_init() { File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: unsafe.Slice(unsafe.StringData(file_gochat_v1_auth_proto_rawDesc), len(file_gochat_v1_auth_proto_rawDesc)), - NumEnums: 0, + NumEnums: 1, NumMessages: 46, NumExtensions: 0, NumServices: 1, }, GoTypes: file_gochat_v1_auth_proto_goTypes, DependencyIndexes: file_gochat_v1_auth_proto_depIdxs, + EnumInfos: file_gochat_v1_auth_proto_enumTypes, MessageInfos: file_gochat_v1_auth_proto_msgTypes, }.Build() File_gochat_v1_auth_proto = out.File diff --git a/api/protobuf/proto/gochat/v1/auth.proto b/api/protobuf/proto/gochat/v1/auth.proto index 7ab471d..95b41e8 100644 --- a/api/protobuf/proto/gochat/v1/auth.proto +++ b/api/protobuf/proto/gochat/v1/auth.proto @@ -252,6 +252,23 @@ message FinishSSOResponse { string provider_name = 6; // The team an invite joined, for a new account. Team team = 7; + // With an app_code: what it does and the GoChat account it is for, which + // the browser asks the person to confirm before the app gets the code. + AppCodeUse app_code_use = 9; + string app_username = 10; + // The account's email at the provider, when it gives one. + string email = 11; +} + +// What an app's code does. +enum AppCodeUse { + APP_CODE_USE_UNSPECIFIED = 0; + // Signs the app in. + APP_CODE_USE_SIGN_IN = 1; + // Links the account at the provider to the app's. + APP_CODE_USE_LINK = 2; + // Proves to the app that its account's owner signed in again. + APP_CODE_USE_REAUTH = 3; } message RedeemSSORequest { diff --git a/internal/server/drpc/sso.go b/internal/server/drpc/sso.go index 0671869..5d6ff0e 100644 --- a/internal/server/drpc/sso.go +++ b/internal/server/drpc/sso.go @@ -59,6 +59,7 @@ func (s *authService) FinishSSO(ctx context.Context, req *pb.FinishSSORequest) ( out.Result = &pb.FinishSSOResponse_ReauthToken{ReauthToken: res.ReauthToken} case res.AppCode != "": out.Result = &pb.FinishSSOResponse_AppCode{AppCode: res.AppCode} + out.AppCodeUse, out.AppUsername, out.Email = res.AppUse, res.AppUsername, res.Email case res.Login != nil && res.Login.MFAToken != "": out.Result = &pb.FinishSSOResponse_MfaToken{MfaToken: res.Login.MFAToken} case res.Login != nil: diff --git a/internal/services/accounts/delete_test.go b/internal/services/accounts/delete_test.go index bd23e39..811347f 100644 --- a/internal/services/accounts/delete_test.go +++ b/internal/services/accounts/delete_test.go @@ -12,6 +12,7 @@ import ( "github.com/gocql/gocql" potp "github.com/pquerna/otp/totp" + pb "github.com/ssenerg/gochat/api/protobuf/gen/gochat/v1" "github.com/ssenerg/gochat/internal/core/ids" "github.com/ssenerg/gochat/internal/core/models" "github.com/ssenerg/gochat/internal/core/secure" @@ -311,7 +312,7 @@ func TestAppsSignInAgainToDelete(t *testing.T) { verifier := secure.Token() + secure.Token() appReauth := func() string { res, err := f.sso(idp, person{}, BeginSSO{AppChallenge: s256(verifier), Reauth: true}, FinishSSO{}) - if err != nil || res.AppCode == "" || res.Login != nil { + if err != nil || res.AppCode == "" || res.Login != nil || res.AppUse != pb.AppCodeUse_APP_CODE_USE_REAUTH || res.AppUsername != "ann" { t.Fatalf("app reauth: %v %+v", err, res) } return res.AppCode diff --git a/internal/services/accounts/messages.go b/internal/services/accounts/messages.go index 5503289..21eb124 100644 --- a/internal/services/accounts/messages.go +++ b/internal/services/accounts/messages.go @@ -183,6 +183,12 @@ type SSOResult struct { ReauthToken string // Provider is the provider's name. Provider string + // Email is the account's at the provider. + Email string + // AppUse and AppUsername say what an AppCode does and whose account it + // is for: the browser asks the person before the app gets the code. + AppUse pb.AppCodeUse + AppUsername string // User, InviteTeamID and InviteHash are of a new account and its invite. User *models.User InviteTeamID gocql.UUID diff --git a/internal/services/accounts/sso.go b/internal/services/accounts/sso.go index ecb707c..966e364 100644 --- a/internal/services/accounts/sso.go +++ b/internal/services/accounts/sso.go @@ -333,7 +333,7 @@ func (w *worker) finishSSO(ctx context.Context, msg FinishSSO) (*SSOResult, erro return nil, err } } - out := &SSOResult{Provider: p.cfg.Name} + out := &SSOResult{Provider: p.cfg.Name, Email: id.Email} if f.Reauth { if !w.signedInSince(c, f) { @@ -349,11 +349,10 @@ func (w *worker) finishSSO(ctx context.Context, msg FinishSSO) (*SSOResult, erro if known && userID != f.LinkUser { return nil, errLinkedElsewhere(p.cfg.Name) } - out.AppCode, err = w.ticket(ctx, &models.SSOTicket{ + return w.forApp(ctx, out, pb.AppCodeUse_APP_CODE_USE_LINK, nil, &models.SSOTicket{ Kind: models.SSOTicketAppLink, UserID: f.LinkUser, Challenge: f.AppChallenge, Provider: p.cfg.ID, Issuer: id.Issuer, Subject: id.Subject, Email: id.Email, }, ssoTicketTTL) - return out, err } if msg.P == nil || msg.P.UserID != f.LinkUser { return nil, ierrors.New(ierrors.CodePermissionDenied, "sign in to the account you were linking, then link it again") @@ -397,8 +396,7 @@ func (w *worker) finishSSO(ctx context.Context, msg FinishSSO) (*SSOResult, erro } w.Logger.Info("sso sign-in", zap.String("user", user.ID.String()), zap.String("provider", p.cfg.ID)) if f.AppChallenge != "" { - out.AppCode, err = w.ticket(ctx, &models.SSOTicket{Kind: models.SSOTicketApp, UserID: user.ID, Challenge: f.AppChallenge, Provider: p.cfg.ID}, ssoTicketTTL) - return out, err + return w.forApp(ctx, out, pb.AppCodeUse_APP_CODE_USE_SIGN_IN, user, &models.SSOTicket{Kind: models.SSOTicketApp, UserID: user.ID, Challenge: f.AppChallenge, Provider: p.cfg.ID}, ssoTicketTTL) } out.Login, err = w.signIn(ctx, user, msg.IP, msg.UserAgent) return out, err @@ -449,15 +447,30 @@ func (w *worker) reauthed(ctx context.Context, p *ssoProvider, f *models.SSOFlow if !ids.IsZero(f.LinkUser) && (caller == nil || caller.UserID != f.LinkUser) { return nil, ierrors.New(ierrors.CodePermissionDenied, "sign in to the account you were deleting, then try again") } - token, err := w.ticket(ctx, &models.SSOTicket{Kind: models.SSOTicketReauth, UserID: userID, Challenge: f.AppChallenge, Provider: p.cfg.ID}, reauthTTL) + t := &models.SSOTicket{Kind: models.SSOTicketReauth, UserID: userID, Challenge: f.AppChallenge, Provider: p.cfg.ID} + if f.AppChallenge != "" { + return w.forApp(ctx, out, pb.AppCodeUse_APP_CODE_USE_REAUTH, nil, t, reauthTTL) + } + out.ReauthToken, err = w.ticket(ctx, t, reauthTTL) + return out, err +} + +// forApp gives an app's code, with what it does and the account it is for +// (user, or else read): a link anyone sends can begin an app's flow, so +// the browser asks the person before the app gets it. +func (w *worker) forApp(ctx context.Context, out *SSOResult, use pb.AppCodeUse, user *models.User, t *models.SSOTicket, ttl time.Duration) (*SSOResult, error) { + if user == nil { + u, err := w.Repo.GetUser(ctx, t.UserID) + if err != nil { + return nil, err + } + user = u + } + code, err := w.ticket(ctx, t, ttl) if err != nil { return nil, err } - if f.AppChallenge != "" { - out.AppCode = token - } else { - out.ReauthToken = token - } + out.AppCode, out.AppUse, out.AppUsername = code, use, user.Username return out, nil } diff --git a/internal/services/accounts/sso_test.go b/internal/services/accounts/sso_test.go index 10c7df4..fb0a4cb 100644 --- a/internal/services/accounts/sso_test.go +++ b/internal/services/accounts/sso_test.go @@ -511,7 +511,7 @@ func TestSSOLinksForAnApp(t *testing.T) { link := func(p *models.Principal, who person) string { t.Helper() res, err := f.sso(idp, who, BeginSSO{AppChallenge: challenge, LinkTicket: start(p)}, FinishSSO{}) - if err != nil || res.AppCode == "" || res.Linked || res.Login != nil { + if err != nil || res.AppCode == "" || res.Linked || res.Login != nil || res.AppUse != pb.AppCodeUse_APP_CODE_USE_LINK { t.Fatalf("app link: %v %+v", err, res) } return res.AppCode @@ -580,7 +580,8 @@ func TestSSOLinksForAnApp(t *testing.T) { } // The browser's own session has no say in an app's link. res, err := f.sso(idp, person{claims: map[string]any{"sub": "carol-2"}}, BeginSSO{AppChallenge: challenge, LinkTicket: start(cp)}, FinishSSO{P: dp}) - if err != nil || res.AppCode == "" || res.Linked { + // The page that asks before the app gets the code names whose it is. + if err != nil || res.AppCode == "" || res.Linked || res.AppUsername != "carol" { t.Fatalf("a browser signed in as dave changed carol's link: %v %+v", err, res) } if _, err := ask[*SSOLinkInfo](f, LinkSSO{P: dp, Token: res.AppCode, Verifier: verifier}); !ierrors.Is(err, ierrors.CodePermissionDenied) { @@ -719,7 +720,7 @@ func TestSSOHandsOffToApps(t *testing.T) { verifier := secure.Token() + secure.Token() appSignIn := func() string { res, err := f.sso(idp, person{}, BeginSSO{AppChallenge: s256(verifier)}, FinishSSO{}) - if err != nil || res.Login != nil || res.AppCode == "" { + if err != nil || res.Login != nil || res.AppCode == "" || res.AppUse != pb.AppCodeUse_APP_CODE_USE_SIGN_IN || res.AppUsername != "ann" || res.Email != "ann@example.test" { t.Fatalf("app sign-in: %v %+v", err, res) } return res.AppCode diff --git a/internal/web/exits_test.go b/internal/web/exits_test.go index ba1af7d..5ed394e 100644 --- a/internal/web/exits_test.go +++ b/internal/web/exits_test.go @@ -224,7 +224,7 @@ func TestAnAppSignsInAgainToDelete(t *testing.T) { if got := auth.begun[0]; got.AppChallenge != challenge || !got.Reauth { t.Fatalf("begin: %+v", got) } - b.expect(b.do(http.MethodGet, ssoCallback+"?code=app&state="+state, nil), http.StatusSeeOther, appRedirect+"?code=app-code") + b.appPage(b.do(http.MethodGet, ssoCallback+"?code=app-reauth&state="+state, nil), "Tell the GoChat app that you, @ann, signed in again with Corp ID") b.toProvider("/login/sso/corp?app=" + challenge) if auth.begun[1].Reauth { t.Fatal("a sign-in asked to sign in again") diff --git a/internal/web/sso.go b/internal/web/sso.go index 7ec651b..302e650 100644 --- a/internal/web/sso.go +++ b/internal/web/sso.go @@ -1,6 +1,7 @@ package web import ( + "html/template" "net/http" "net/url" "regexp" @@ -22,7 +23,8 @@ const ( // reauthTTL is how long the server takes a sign-in again. reauthTTL = 5 * time.Minute // appRedirect is where an app's sign-in comes back: a scheme the apps - // claim, which then redeem the code with their PKCE verifier. + // claim, which then redeem the code with their PKCE verifier. Any app + // may claim it, so a code goes only once the person says so. appRedirect = "io.github.ssenerg.gochat:/sso" ssoCallback = "/login/sso/callback" ) @@ -95,15 +97,30 @@ func (h *Handler) ssoFlow(r *http.Request) (ssoFlow, bool) { return f, true } -// toApp sends an app's browser back to it, with the code or why not. -func toApp(w http.ResponseWriter, r *http.Request, key, value string) { - http.Redirect(w, r, appRedirect+"?"+url.Values{key: {value}}.Encode(), http.StatusSeeOther) +func appURL(key, value string) string { + return appRedirect + "?" + url.Values{key: {value}}.Encode() +} + +// ssoAppData asks the person before an app gets its code: a link anyone +// sends can begin an app's flow, and another app may claim the scheme. +type ssoAppData struct { + Page + Use, Provider, Username, Email string + Href template.URL +} + +func (h *Handler) ssoAppPage(w http.ResponseWriter, r *http.Request, res *pb.FinishSSOResponse, code string) { + use := map[pb.AppCodeUse]string{pb.AppCodeUse_APP_CODE_USE_LINK: "link", pb.AppCodeUse_APP_CODE_USE_REAUTH: "reauth"}[res.AppCodeUse] + h.page(w, r, http.StatusOK, "sso-app", ssoAppData{ + Page: Page{Title: "Continue in the app"}, Use: use, Provider: res.ProviderName, Username: res.AppUsername, Email: res.Email, + Href: template.URL(appURL("code", code)), //nolint:gosec // the apps' scheme, with an escaped query + }) } // ssoFailed shows why a sign-in failed, on the sign-in page or in the app. func (h *Handler) ssoFailed(w http.ResponseWriter, r *http.Request, f ssoFlow, status int, msg string) { if f.App { - toApp(w, r, "error", msg) + http.Redirect(w, r, appURL("error", msg), http.StatusSeeOther) return } h.page(w, r, status, "login", h.loginData(w, r, f.Next, msg)) @@ -198,7 +215,7 @@ func (h *Handler) ssoCallbackPage(w http.ResponseWriter, r *http.Request) { } switch out := res.Result.(type) { case *pb.FinishSSOResponse_AppCode: - toApp(w, r, "code", out.AppCode) + h.ssoAppPage(w, r, res, out.AppCode) case *pb.FinishSSOResponse_Session: h.startSession(w, out.Session) http.Redirect(w, r, h.afterSignIn(r, next), http.StatusSeeOther) diff --git a/internal/web/sso_flow_test.go b/internal/web/sso_flow_test.go index 3364c2f..e1fee99 100644 --- a/internal/web/sso_flow_test.go +++ b/internal/web/sso_flow_test.go @@ -63,8 +63,10 @@ func (f *fakeAuth) FinishSSO(_ context.Context, req *pb.FinishSSORequest) (*pb.F out.Result = &pb.FinishSSOResponse_Session{Session: f.newSession()} case "link": out.Result = &pb.FinishSSOResponse_LinkToken{LinkToken: strings.Repeat("L", 43)} - case "app": + case "app", "app-link", "app-reauth": + use := map[string]pb.AppCodeUse{"app": pb.AppCodeUse_APP_CODE_USE_SIGN_IN, "app-link": pb.AppCodeUse_APP_CODE_USE_LINK, "app-reauth": pb.AppCodeUse_APP_CODE_USE_REAUTH}[req.Code] out.Result = &pb.FinishSSOResponse_AppCode{AppCode: "app-code"} + out.AppCodeUse, out.AppUsername, out.Email = use, "ann", "ann@corp.test" default: return nil, rpcErr(ierrors.CodePermissionDenied, "no account is linked") } @@ -161,6 +163,15 @@ func (b *browser) expect(w *httptest.ResponseRecorder, status int, location stri } } +// appPage checks the page that asks before an app gets its code. +func (b *browser) appPage(w *httptest.ResponseRecorder, says string) { + b.t.Helper() + body := w.Body.String() + if w.Code != http.StatusOK || w.Header().Get("Location") != "" || !strings.Contains(body, `href="`+appRedirect+`?code=app-code"`) || !strings.Contains(body, says) { + b.t.Fatalf("got %d to %q, want the page saying %q: %s", w.Code, w.Header().Get("Location"), says, body) + } +} + func newFlow(t *testing.T) (*browser, *fakeAuth) { t.Helper() auth := &fakeAuth{} @@ -285,7 +296,9 @@ func TestSSOHandsTheCodeToTheApp(t *testing.T) { if auth.begun[0].AppChallenge != challenge { t.Fatalf("begin: %+v", auth.begun[0]) } - b.expect(b.do(http.MethodGet, ssoCallback+"?code=app&state="+state, nil), http.StatusSeeOther, appRedirect+"?code=app-code") + // A link anyone sends can begin an app's sign-in, so the code waits + // for the person to say it is theirs. + b.appPage(b.do(http.MethodGet, ssoCallback+"?code=app&state="+state, nil), "Sign in to the GoChat app as @ann with your Corp ID account ann@corp.test?") state = b.toProvider("/login/sso/corp?app=" + challenge) b.expect(b.do(http.MethodGet, ssoCallback+"?code=nobody&state="+state, nil), http.StatusSeeOther, appRedirect+"?error=no+account+is+linked") if b.cookies["gochat_session"] != nil { @@ -297,5 +310,5 @@ func TestSSOHandsTheCodeToTheApp(t *testing.T) { if got := auth.begun[len(auth.begun)-1]; got.AppChallenge != challenge || got.LinkTicket != ticket || got.Reauth { t.Fatalf("begin: %+v", got) } - b.expect(b.do(http.MethodGet, ssoCallback+"?code=app&state="+state, nil), http.StatusSeeOther, appRedirect+"?code=app-code") + b.appPage(b.do(http.MethodGet, ssoCallback+"?code=app-link&state="+state, nil), "Sign in to @ann with your Corp ID account ann@corp.test from now on") } diff --git a/internal/web/views/templates/pages/sso-app.html b/internal/web/views/templates/pages/sso-app.html new file mode 100644 index 0000000..9a34953 --- /dev/null +++ b/internal/web/views/templates/pages/sso-app.html @@ -0,0 +1,19 @@ +{{define "content"}} +
+
+
GoChat
+ {{if eq .Use "link"}} +

Link your account

+

Sign in to @{{.Username}} with your {{.Provider}} account{{with .Email}} {{.}}{{end}} from now on, as the GoChat app asked?

+ {{else if eq .Use "reauth"}} +

Confirm it is you

+

Tell the GoChat app that you, @{{.Username}}, signed in again with {{.Provider}}, so it can delete your account?

+ {{else}} +

Sign in to the app

+

Sign in to the GoChat app{{with .Username}} as @{{.}}{{end}} with your {{.Provider}} account{{with .Email}} {{.}}{{end}}?

+ {{end}} +

Continue only if you began this in the GoChat app on this device a moment ago. If someone sent you a link, close this page: continuing could hand them your account.

+ Continue in the app +
+
+{{end}} diff --git a/mobile/README.md b/mobile/README.md index 5793a02..594d49d 100644 --- a/mobile/README.md +++ b/mobile/README.md @@ -57,9 +57,12 @@ iosApp/ the Xcode project: a SwiftUI shell that shows the shared screens - Sign in to any GoChat server, with two-factor codes, or sign up with an invite link. Where the server has identity providers, sign in (or up) with one in the system browser: a Custom Tab on Android, an - `ASWebAuthenticationSession` on iOS. It comes back to the app at + `ASWebAuthenticationSession` on iOS. Once the person confirms the + account on the server's page, it comes back to the app at `io.github.ssenerg.gochat:/sso`, which Android's activity claims, and the app trades the code for a session with the PKCE verifier it began with. + The server always asks first: anyone can send a link that begins a + flow, and another app may claim the scheme. - Teams, channels, voice channels, direct and group conversations, with presence, unread marks and mention counts, all live. - Voice channels: join, talk, mute, deafen and leave, see who is talking,