Skip to content

Commit

Permalink
Added ability to format OTP with different patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
jctim committed Jul 27, 2018
1 parent c9429c6 commit 7f40eee
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 7 deletions.
3 changes: 3 additions & 0 deletions resources/settings/properties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<property id="ForegroundColor" type="number">0xFFFFFF</property>
<property id="CircleTimerColor" type="number">0x109AD7</property>
<property id="CircleTimerArrows" type="boolean">false</property>
<property id="OTPFormatFor6" type="number">0</property>
<property id="OTPFormatFor7" type="number">0</property>
<property id="OTPFormatFor8" type="number">0</property>

<property id="Account1Enabled" type="boolean">false</property>
<property id="Account2Enabled" type="boolean">false</property>
Expand Down
29 changes: 29 additions & 0 deletions resources/settings/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,35 @@
<settingConfig type="boolean" />
</setting>

<setting propertyKey="@Properties.OTPFormatFor6" title="@Strings.OTPFormatFor6">
<settingConfig type="list">
<listEntry value="0">@Strings.OTPFormatFor6_xxxxxx</listEntry>
<listEntry value="1">@Strings.OTPFormatFor6_xxx_xxx</listEntry>
<listEntry value="2">@Strings.OTPFormatFor6_xx_xx_xx</listEntry>
</settingConfig>
</setting>

<setting propertyKey="@Properties.OTPFormatFor7" title="@Strings.OTPFormatFor7">
<settingConfig type="list">
<listEntry value="0">@Strings.OTPFormatFor7_yyyyyyy</listEntry>
<listEntry value="1">@Strings.OTPFormatFor7_yyy_yyyy</listEntry>
<listEntry value="2">@Strings.OTPFormatFor7_yyyy_yyy</listEntry>
<listEntry value="3">@Strings.OTPFormatFor7_yyy_y_yyy</listEntry>
<listEntry value="4">@Strings.OTPFormatFor7_yy_yyy_yy</listEntry>
</settingConfig>
</setting>

<setting propertyKey="@Properties.OTPFormatFor8" title="@Strings.OTPFormatFor8">
<settingConfig type="list">
<listEntry value="0">@Strings.OTPFormatFor8_zzzzzzzz</listEntry>
<listEntry value="1">@Strings.OTPFormatFor8_zzzz_zzzz</listEntry>
<listEntry value="2">@Strings.OTPFormatFor8_zzz_zz_zzz</listEntry>
<listEntry value="3">@Strings.OTPFormatFor8_zz_zzzz_zz</listEntry>
<listEntry value="4">@Strings.OTPFormatFor8_zz_zz_zz_zz</listEntry>
</settingConfig>
</setting>


<!-- Account 1 -->
<setting propertyKey="@Properties.Account1Enabled" title="@Strings.Account1">
<settingConfig type="boolean" />
Expand Down
20 changes: 20 additions & 0 deletions resources/strings/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@
<string id="CircleTimerColorTitle">Time Circle Color</string>
<string id="CircleTimerArrowsTitle">Use Arrows in Circle Timer</string>

<string id="OTPFormatFor6">The format for 6-numeric OTP</string>
<string id="OTPFormatFor7">The format for 7-numeric OTP</string>
<string id="OTPFormatFor8">The format for 8-numeric OTP</string>

<string id="OTPFormatFor6_xxxxxx">123456</string>
<string id="OTPFormatFor6_xxx_xxx">123 456</string>
<string id="OTPFormatFor6_xx_xx_xx">12 34 56</string>

<string id="OTPFormatFor7_yyyyyyy">1234567</string>
<string id="OTPFormatFor7_yyy_yyyy">123 4567</string>
<string id="OTPFormatFor7_yyyy_yyy">1234 567</string>
<string id="OTPFormatFor7_yyy_y_yyy">123 4 567</string>
<string id="OTPFormatFor7_yy_yyy_yy">12 345 67</string>

<string id="OTPFormatFor8_zzzzzzzz">12345678</string>
<string id="OTPFormatFor8_zzzz_zzzz">1234 5678</string>
<string id="OTPFormatFor8_zzz_zz_zzz">123 45 678</string>
<string id="OTPFormatFor8_zz_zzzz_zz">12 3456 78</string>
<string id="OTPFormatFor8_zz_zz_zz_zz">12 34 56 78</string>

<string id="ColorBlack">Black</string>
<string id="ColorDarkGray">Dark Gray</string>
<string id="ColorLightGray">Light Gray</string>
Expand Down
13 changes: 6 additions & 7 deletions source/OtpDataProvider.mc
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,12 @@ class OtpDataProvider {
}

function formatToken(token) {
var formattedToken = "";
switch(token.length()) {
case 6: formattedToken = token.substring(0, 3) + " " + token.substring(3, 7); break;
case 7: formattedToken = token.substring(0, 2) + " " + token.substring(2, 5) + " " + token.substring(5, 8); break;
case 8: formattedToken = token.substring(0, 3) + " " + token.substring(3, 5) + " " + token.substring(5, 9); break;
}
return formattedToken;
var tokenLength = token.length();
var formatIndex = AppData.readProperty("OTPFormatFor" + tokenLength);
var formatPattern = Constants.OTP_FORMAT.get(tokenLength).get(formatIndex);

Sys.println("Formatting token " + token + " with pattern " + formatPattern);
return Lang.format(formatPattern, token.toCharArray());
}

function getCurrentOtp() {
Expand Down
22 changes: 22 additions & 0 deletions source/util/Constants.mc
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,26 @@ module Constants {
const FG_COLOR_PROP = "ForegroundColor";
const CIRCLE_TIMER_COLOR_PROP = "CircleTimerColor";
const CIRCLE_TIMER_ARROWS_PROP = "CircleTimerArrows";

const OTP_FORMAT = {
6 => {
0 => "$1$$2$$3$$4$$5$$6$", // OTPFormatFor6_xxxxxx
1 => "$1$$2$$3$ $4$$5$$6$", // OTPFormatFor6_xxx_xxx
2 => "$1$$2$ $3$$4$ $5$$6$" // OTPFormatFor6_xx_xx_xx
},
7 => {
0 => "$1$$2$$3$$4$$5$$6$$7$", // OTPFormatFor7_yyyyyyy
1 => "$1$$2$$3$ $4$$5$$6$$7$", // OTPFormatFor7_yyy_yyyy
2 => "$1$$2$$3$$4$ $5$$6$$7$", // OTPFormatFor7_yyyy_yyy
3 => "$1$$2$$3$ $4$ $5$$6$$7$", // OTPFormatFor7_yyy_y_yyy
4 => "$1$$2$ $3$$4$$5$ $6$$7$" // OTPFormatFor7_yy_yyy_yy
},
8 => {
0 => "$1$$2$$3$$4$$5$$6$$7$$8$", // OTPFormatFor8_zzzzzzzz
1 => "$1$$2$$3$$4$ $5$$6$$7$$8$", // OTPFormatFor8_zzzz_zzzz
2 => "$1$$2$$3$ $4$$5$ $6$$7$$8$", // OTPFormatFor8_zzz_zz_zzz
3 => "$1$$2$ $3$$4$$5$$6$ $7$$8$", // OTPFormatFor8_zz_zzzz_zz
4 => "$1$$2$ $3$$4$ $5$$6$ $7$$8$" // OTPFormatFor8_zz_zz_zz_zz
}
};
}

0 comments on commit 7f40eee

Please sign in to comment.