Skip to content

Commit

Permalink
Merge pull request #175 from dekart-xyz/fix-asking-loggin-every-time
Browse files Browse the repository at this point in the history
Add login hint for Google OAuth
  • Loading branch information
delfrrr committed Apr 10, 2024
2 parents e609256 + aac0d5c commit 1c678a3
Show file tree
Hide file tree
Showing 10 changed files with 391 additions and 323 deletions.
1 change: 1 addition & 0 deletions proto/dekart.proto
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ message AuthState {
string access_token_to_revoke = 4; // access token to revoke
bool switch_account = 5; // if true, user will be requested to switch account
bool sensitive_scope = 6; // if true, user will be requested to grant sensitive scope
string login_hint = 7; // login hint for Google OAuth
}

message ArchiveReportRequest {
Expand Down
3 changes: 2 additions & 1 deletion src/client/actions/localStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
const LOCAL_STORAGE_KEY = 'dekart-local-storage-v1'

const initialState = {
sensitiveScopesGrantedOnce: false
sensitiveScopesGrantedOnce: false,
loginHint: null
}

let current = initialState
Expand Down
6 changes: 5 additions & 1 deletion src/client/actions/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ const { REACT_APP_API_HOST } = process.env // this never changes, passed during

// authRedirect will redirect the browser to the authentication endpoint
export function authRedirect (state) {
return async (dispatch) => {
return async (dispatch, getState) => {
dispatch({ type: authRedirect.name })
const loginHint = getState().user.loginHint
if (loginHint) {
state.setLoginHint(loginHint)
}
const req = new URL('/api/v1/authenticate', REACT_APP_API_HOST || window.location.href)
state.setAuthUrl(req.href)
const stateBase64 = btoa(String.fromCharCode.apply(null, state.serializeBinary()))
Expand Down
1 change: 1 addition & 0 deletions src/client/actions/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function subscribeUserStream () {
dispatch(grpcStream(Dekart.GetUserStream, request, (message, err) => {
if (message) {
dispatch(updateLocalStorage('sensitiveScopesGrantedOnce', message.sensitiveScopesGrantedOnce))
dispatch(updateLocalStorage('loginHint', message.email))
dispatch(userStreamUpdate(message))
if (prevRes.connectionUpdate !== message.connectionUpdate) {
prevRes.connectionUpdate = message.connectionUpdate
Expand Down
14 changes: 13 additions & 1 deletion src/client/reducers/userReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,19 @@ function sensitiveScopesGrantedOnce (state = false, action) {
}
}

function loginHint (state = null, action) {
switch (action.type) {
case localStorageInit.name:
return action.current.loginHint || state
case userStreamUpdate.name:
return action.userStream.email
default:
return state
}
}

export default combineReducers({
stream,
sensitiveScopesGrantedOnce
sensitiveScopesGrantedOnce,
loginHint
})
648 changes: 329 additions & 319 deletions src/proto/dekart.pb.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/proto/dekart_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,9 @@ export class AuthState extends jspb.Message {
getSensitiveScope(): boolean;
setSensitiveScope(value: boolean): void;

getLoginHint(): string;
setLoginHint(value: string): void;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): AuthState.AsObject;
static toObject(includeInstance: boolean, msg: AuthState): AuthState.AsObject;
Expand All @@ -685,6 +688,7 @@ export namespace AuthState {
accessTokenToRevoke: string,
switchAccount: boolean,
sensitiveScope: boolean,
loginHint: string,
}

export interface ActionMap {
Expand Down
32 changes: 31 additions & 1 deletion src/proto/dekart_pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -5569,7 +5569,8 @@ proto.AuthState.toObject = function(includeInstance, msg) {
uiUrl: jspb.Message.getFieldWithDefault(msg, 3, ""),
accessTokenToRevoke: jspb.Message.getFieldWithDefault(msg, 4, ""),
switchAccount: jspb.Message.getBooleanFieldWithDefault(msg, 5, false),
sensitiveScope: jspb.Message.getBooleanFieldWithDefault(msg, 6, false)
sensitiveScope: jspb.Message.getBooleanFieldWithDefault(msg, 6, false),
loginHint: jspb.Message.getFieldWithDefault(msg, 7, "")
};

if (includeInstance) {
Expand Down Expand Up @@ -5630,6 +5631,10 @@ proto.AuthState.deserializeBinaryFromReader = function(msg, reader) {
var value = /** @type {boolean} */ (reader.readBool());
msg.setSensitiveScope(value);
break;
case 7:
var value = /** @type {string} */ (reader.readString());
msg.setLoginHint(value);
break;
default:
reader.skipField();
break;
Expand Down Expand Up @@ -5701,6 +5706,13 @@ proto.AuthState.serializeBinaryToWriter = function(message, writer) {
f
);
}
f = message.getLoginHint();
if (f.length > 0) {
writer.writeString(
7,
f
);
}
};


Expand Down Expand Up @@ -5822,6 +5834,24 @@ proto.AuthState.prototype.setSensitiveScope = function(value) {
};


/**
* optional string login_hint = 7;
* @return {string}
*/
proto.AuthState.prototype.getLoginHint = function() {
return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 7, ""));
};


/**
* @param {string} value
* @return {!proto.AuthState} returns this
*/
proto.AuthState.prototype.setLoginHint = function(value) {
return jspb.Message.setProto3StringField(this, 7, value);
};





Expand Down
3 changes: 3 additions & 0 deletions src/server/dekart/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ func (s Server) getConnections(ctx context.Context) ([]*proto.Connection, error)
from connections where archived=false order by created_at desc`,
)
if err != nil {
if err == context.Canceled {
return nil, err
}
log.Fatal().Err(err).Msg("select from connections failed")
}
defer rows.Close()
Expand Down
2 changes: 2 additions & 0 deletions src/server/user/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ func (c ClaimsCheck) Authenticate(w http.ResponseWriter, r *http.Request) {
var url string
if state.GetSwitchAccount() {
url = auth.AuthCodeURL(stateBase64, oauth2.SetAuthURLParam("prompt", "select_account"))
} else if state.LoginHint != "" {
url = auth.AuthCodeURL(stateBase64, oauth2.SetAuthURLParam("login_hint", state.LoginHint))
} else {
url = auth.AuthCodeURL(stateBase64)
}
Expand Down

0 comments on commit 1c678a3

Please sign in to comment.