Skip to content

Commit 4cb8b4d

Browse files
authored
wings & wax. use new instagram APIs (#152)
* sun, wax & wings. fix ig oauth flow * wings. fix instagram login form * sun & wax. remove comments
1 parent a79043d commit 4cb8b4d

File tree

5 files changed

+35
-45
lines changed

5 files changed

+35
-45
lines changed

sun/sun-api/models/social.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@ type LinkedInRespToken struct {
4747

4848
type InstagramRespToken struct {
4949
AccessToken string `json:"access_token"`
50-
User struct {
51-
Id string `json:"id"`
52-
Username string `json:"username"`
53-
FullName string `json:"full_name"`
54-
ProfilePicture string `json:"profile_picture"`
55-
} `json:"user"`
50+
UserId int `json:"user_id"`
5651
}
5752

5853
type FacebookInspectToken struct {
@@ -99,18 +94,6 @@ type LinkedinEmailDetail struct {
9994
}
10095

10196
type InstagramUserDetail struct {
102-
Data struct {
103-
Id string `json:"id"`
104-
Username string `json:"username"`
105-
FullName string `json:"full_name"`
106-
ProfilePicture string `json:"profile_picture"`
107-
Bio string `json:"bio"`
108-
Website string `json:"website"`
109-
IsBusiness bool `json:"is_business"`
110-
Counts struct {
111-
Media int `json:"media"`
112-
Follows int `json:"follows"`
113-
FollowedBy int `json:"followed_by"`
114-
} `json:"counts"`
115-
} `json:"data"`
97+
Id string `json:"id"`
98+
Username string `json:"username"`
11699
}

sun/sun-tasks/tasks/tasks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ func storeSessions() {
117117

118118
func cleanOldSessions() {
119119
db := database.Instance()
120-
db.Where("start_time < ?", time.Now().AddDate(0, -6, 0).UTC()).Delete(models.Session{})
121-
db.Where("start_time < ?", time.Now().AddDate(0, -6, 0).UTC()).Delete(models.SessionHistory{})
120+
db.Where("update_time < ?", time.Now().AddDate(0, -6, 0).UTC()).Delete(models.Session{})
121+
db.Where("update_time < ?", time.Now().AddDate(0, -6, 0).UTC()).Delete(models.SessionHistory{})
122122
}
123123

124124
func cleanShortUrls() {

wax/methods/auth_social.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,8 @@ func InstagramAuth(c *gin.Context) {
502502
}
503503

504504
// extract user info
505-
urlAPI := "https://api.instagram.com/v1/users/self/?access_token=" + inRespToken.AccessToken
505+
u := strconv.Itoa(inRespToken.UserId)
506+
urlAPI := "https://graph.instagram.com/"+u+"?fields=id,username&access_token=" + inRespToken.AccessToken
506507

507508
resp, err = client.Get(urlAPI)
508509
if err != nil {
@@ -517,14 +518,14 @@ func InstagramAuth(c *gin.Context) {
517518
fmt.Println(err.Error())
518519
}
519520

520-
if inUserDetail.Data.Id == "" {
521+
if inUserDetail.Id == "" {
521522
c.JSON(http.StatusBadRequest, gin.H{"message": "access token is invalid"})
522523
return
523524
}
524525

525526
// check if user exists
526527
unit := utils.GetUnitByUuid(uuid)
527-
user := utils.GetUserByUsernameAndHotspot(inUserDetail.Data.Id, unit.HotspotId)
528+
user := utils.GetUserByUsernameAndHotspot(inUserDetail.Id, unit.HotspotId)
528529
if user.Id == 0 {
529530
// create user
530531
days := utils.GetHotspotPreferencesByKey(unit.HotspotId, "user_expiration_days")
@@ -560,8 +561,8 @@ func InstagramAuth(c *gin.Context) {
560561

561562
newUser := models.User{
562563
HotspotId: unit.HotspotId,
563-
Name: inUserDetail.Data.FullName,
564-
Username: inUserDetail.Data.Id,
564+
Name: inUserDetail.Username,
565+
Username: inUserDetail.Id,
565566
Password: "",
566567
Email: "",
567568
AccountType: "instagram",
@@ -583,10 +584,10 @@ func InstagramAuth(c *gin.Context) {
583584
utils.CreateUserSession(newUser.Id, sessionId)
584585

585586
// create marketing info with user infos
586-
utils.CreateUserMarketing(newUser.Id, inUserDetail.Data, "instagram")
587+
utils.CreateUserMarketing(newUser.Id, inUserDetail, "instagram")
587588

588589
// response to client
589-
c.JSON(http.StatusOK, gin.H{"user_id": inUserDetail.Data.Id, "user_db_id": newUser.Id})
590+
c.JSON(http.StatusOK, gin.H{"user_id": inUserDetail.Id, "user_db_id": newUser.Id})
590591
} else {
591592
// update user info
592593
days := utils.GetHotspotPreferencesByKey(user.HotspotId, "user_expiration_days")
@@ -599,7 +600,7 @@ func InstagramAuth(c *gin.Context) {
599600
utils.CreateUserSession(user.Id, sessionId)
600601

601602
// create marketing info with user infos
602-
utils.CreateUserMarketing(user.Id, inUserDetail.Data, "instagram")
603+
utils.CreateUserMarketing(user.Id, inUserDetail, "instagram")
603604

604605
// retrieve voucher
605606
if len(voucherCode) > 0 {
@@ -614,7 +615,7 @@ func InstagramAuth(c *gin.Context) {
614615
}
615616

616617
// response to client
617-
c.JSON(http.StatusOK, gin.H{"user_id": inUserDetail.Data.Id, "user_db_id": user.Id})
618+
c.JSON(http.StatusOK, gin.H{"user_id": inUserDetail.Id, "user_db_id": user.Id})
618619
}
619620

620621
}

wings/src/components/social/InstagramPage.vue

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default {
7777
if (params.code && params.state) {
7878
// extract wings preferences
7979
this.getPreferences(
80-
this.parseState(params.state),
80+
this.parseState(params.state, true),
8181
function(success) {
8282
this.$parent.hotspot.name = success.body.hotspot_name;
8383
this.$parent.hotspot.disclaimers = success.body.disclaimers;
@@ -110,7 +110,7 @@ export default {
110110
// make request to wax
111111
var url = this.createWaxURL(
112112
params.code,
113-
this.parseState(params.state),
113+
this.parseState(params.state, true),
114114
"social/instagram",
115115
false,
116116
this.$route.query.integration_done ? this.$route.query.user : ""
@@ -135,7 +135,7 @@ export default {
135135
var redirectUrl = this.$root.$options.hotspot.integrations[0]
136136
.post_auth_redirect_url;
137137
138-
var appendParams = this.parseState(params.state);
138+
var appendParams = this.parseState(params.state, true);
139139
140140
var query =
141141
"?digest=" +
@@ -184,7 +184,8 @@ export default {
184184
context.authorized = false;
185185
context.dedaloError = true;
186186
console.error(error);
187-
}
187+
},
188+
true
188189
);
189190
}, 1000);
190191
}
@@ -250,7 +251,7 @@ export default {
250251
if (!this.conditions) {
251252
this.deleteMarketingInfo(
252253
this.userId,
253-
this.parseState(params.state),
254+
this.parseState(params.state, true),
254255
function(success) {
255256
context.accept();
256257
},
@@ -266,7 +267,7 @@ export default {
266267
if (!this.surveys) {
267268
this.deleteSurveyInfo(
268269
this.userId,
269-
this.parseState(params.state),
270+
this.parseState(params.state, true),
270271
function(success) {
271272
context.accept();
272273
},
@@ -290,7 +291,7 @@ export default {
290291
) {
291292
this.addAdditionalInfo(
292293
this.userId,
293-
this.parseState(params.state),
294+
this.parseState(params.state, true),
294295
{
295296
reason: this.additionalReason,
296297
country: this.additionalCountry

wings/src/mixins/auth.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ var AuthMixin = {
5757
nasid: nasid
5858
}
5959
},
60-
parseState: function (state) {
60+
parseState: function (state, base64) {
61+
if (base64) {
62+
state = atob(state)
63+
state = decodeURIComponent(state)
64+
}
65+
6166
var digest = state.split('&')[0]
6267
var uuid = state.split('&')[1]
6368
var sessionid = state.split('&')[2]
@@ -111,21 +116,21 @@ var AuthMixin = {
111116
case 'instagram':
112117
url = 'https://api.instagram.com/oauth/authorize/?' +
113118
'client_id=' + params.in_client_id +
114-
'&state=' + encodeURIComponent(params.digest + "&" + params.uuid + "&" + params.sessionid + "&" + params.uamip + "&" + params.uamport + "&" + params.voucher + "&" + params.nasid) +
115-
'&scope=' + escape('basic') +
116-
'&redirect_uri=' + escape('http://' + window.location.host + '/wings/login/instagram') +
119+
'&state=' + btoa(encodeURIComponent(params.digest + "&" + params.uuid + "&" + params.sessionid + "&" + params.uamip + "&" + params.uamport + "&" + params.voucher + "&" + params.nasid)) +
120+
'&scope=user_profile' +
121+
'&redirect_uri=' + escape('https://' + window.location.host + '/wings/login/instagram') +
117122
'&response_type=code'
118123
break
119124
}
120125
return url
121126
},
122-
doDedaloLogin: function (user, callback) {
127+
doDedaloLogin: function (user, callback, error, base64) {
123128
var params = this.extractParams()
124129
var ip = params.uamip || null
125130
var port = params.uamport || null
126131

127132
if (params.state) {
128-
var state = this.parseState(params.state)
133+
var state = this.parseState(params.state, base64)
129134
ip = state.uamip
130135
port = state.uamport
131136
}
@@ -179,4 +184,4 @@ var AuthMixin = {
179184
}
180185
}
181186
};
182-
export default AuthMixin;
187+
export default AuthMixin;

0 commit comments

Comments
 (0)